如何在DAX中基于月度生产数据创建一个计算列?

huangapple go评论47阅读模式
英文:

How can I create an index calculated column in DAX based on monthly production data?

问题

DAX基于两个列(日期和项目)创建索引的计算列。

你好,社区!

我想在以下数据的右边创建两个DAX计算列:

日期 RunningActMonth RunningMonth
01/01/1990 PP-0001 0 0 0 0 0
01/02/1990 PP-0001 0 0 0 0 0
01/03/1990 PP-0001 0 0 0 0 0
01/04/1990 PP-0001 0 0 0 0 0
01/05/1990 PP-0001 0 0 0 0 0
01/06/1990 PP-0001 0 0 0 0 0
01/07/1990 PP-0001 0 0 0 0 0
01/08/1990 PP-0001 0 0 0 0 0
01/09/1990 PP-0001 0 0.2 0 1 1
01/10/1990 PP-0001 0 5 0 2 2
01/11/1990 PP-0001 1 2 50 3 3
01/12/1990 PP-0001 1.5 4 75 4 4
01/01/1991 PP-0001 3 6 150 5 5
01/02/1991 PP-0001 4.5 18 225 6 6
01/03/1991 PP-0001 6 2 300 7 7
01/04/1991 PP-0001 5 1 250 8 8
01/05/1991 PP-0001 2 3 100 9 9
01/06/1991 PP-0001 0 0 0 9 10
01/07/1991 PP-0001 0 0 0 9 11
01/08/1991 PP-0001 0 0 0 9 12
01/09/1991 PP-0001 0 0 0 9 13
01/10/1991 PP-0001 8 2 400 10 14
01/11/1991 PP-0001 4 3 200 11 15
01/12/1991 PP-0001 3 5 150 12 16

解释:我有几个名为“Wells”的项目。对于每一个项目,我有每月的数据(油、水和气的产量)。对于每口井,我想找到第一个产量数据 > 0,并从那时开始计算一个索引列。

  • 第一列“RunningActMonth”将计算每次产量列中有一个值大于零的时间,但如果产量= 0,则会重复较早的索引。
  • 第二列“RunningMonth”将在第一次生产数据后继续无限索引。

非常感谢你的帮助。
来自阿根廷的问候。

英文:

DAX calculated column for INDEX based on two columns: Date and Item.

Hi Comunity!

I'd like to create the two calculated columns in DAX at the right of the following data:

Date Well Oil Water Gas RunningActMonth RunningMonth
01/01/1990 PP-0001 0 0 0 0 0
01/02/1990 PP-0001 0 0 0 0 0
01/03/1990 PP-0001 0 0 0 0 0
01/04/1990 PP-0001 0 0 0 0 0
01/05/1990 PP-0001 0 0 0 0 0
01/06/1990 PP-0001 0 0 0 0 0
01/07/1990 PP-0001 0 0 0 0 0
01/08/1990 PP-0001 0 0 0 0 0
01/09/1990 PP-0001 0 0.2 0 1 1
01/10/1990 PP-0001 0 5 0 2 2
01/11/1990 PP-0001 1 2 50 3 3
01/12/1990 PP-0001 1.5 4 75 4 4
01/01/1991 PP-0001 3 6 150 5 5
01/02/1991 PP-0001 4.5 18 225 6 6
01/03/1991 PP-0001 6 2 300 7 7
01/04/1991 PP-0001 5 1 250 8 8
01/05/1991 PP-0001 2 3 100 9 9
01/06/1991 PP-0001 0 0 0 9 10
01/07/1991 PP-0001 0 0 0 9 11
01/08/1991 PP-0001 0 0 0 9 12
01/09/1991 PP-0001 0 0 0 9 13
01/10/1991 PP-0001 8 2 400 10 14
01/11/1991 PP-0001 4 3 200 11 15
01/12/1991 PP-0001 3 5 150 12 16

Explanation: I have several Items called "Wells". For each one of them, I have monthly data (oil, water, and gas production). For each well I'd like to find the first production data > 0, and from then start to count an Index column.

  • The first column "RunningActMonth" would count every time one of the production columns is greater than zero, but it will repeat the earlier index if the production = 0.
  • The second column "RunningMonth" would keep indexing forever after the First production data.

Your help will be much appreciated.
Regards from Argentina.

答案1

得分: 2

创建一个列来输出是否有生产。

Production = table[water] > 0 || table[oil] > 0 || table[gas] > 0

我们可以在我们的两列中引用它。

RunningActMonth =
Calculate(
distinctcount( table[date] ),
Filter(
allexcept( table, table[well] ),
Table[date] <= earlier( Table[date] ) && [production]
)
)

这个函数计算所选井上或之前的任何日期的不同日期的数量。

RunningMonth =
Max( 0,
Datediff(
Calculate(
Firstnonblank( table[date], if( [production], true(), blank ()) ),
allexcept( table, table[well] )
),
Table[date],
Month
) + 1
)

这会计算生产值大于零的第一个日期。然后它会取当前月份与该月份之间的非负月份差异。

---EDIT-------------------------

为了使其可扩展,我们可以使用相同的度量单位:

Production = IF( SUM ( table[water] ) = 0 || SUM ( table[oil] ) = 0 || SUM ( table[gas] ) = 0, TRUE(), BLANK() )

我更新了Production,使其评估为BLANK()而不是FALSE(),以加快后续的评估速度。

为了使下一步工作,我们需要一个与生产表关联的日历(日期)表。

RunningActMonth =
CALCULATE(
COUNTROWS(
FILTER(
DATESBETWEEN( 'Calendar'[Date], BLANK(), LASTDATE( 'Calendar'[Date] ) ),
[Production]
)
),
ALLEXCEPT( table, table[well] )
)

RunningMonth =
Max( 0,
DATEDIFF(
CALCULATE(
FIRSTNONBLANK( 'Calendar'[date], [Production] ),
ALLEXCEPT( table, table[well] )
),
LASTDATE( 'Calendar'[Date] ),
Month
) + 1
)

这里唯一的主要变化是引用了日历表,这将加快评估速度。实际上,这将遍历井和日期。您的模型速度将由井的数量乘以您的日历中的日期数量决定。

英文:

Create a column to output whether there is production.

Production = table[water] &gt; 0 || table[oil] &gt; 0 || table[gas] &gt; 0

We can reference that in our two columns.

RunningActMonth = 
Calculate( 
    distinctcount( table[date] ), 
    Filter(
        allexcept( table, table[well] ), 
        Table[date] &lt;= earlier( Table[date] ) &amp;&amp; [production]
    )
)

This one counts the number of distinct dates with any production at the selected well on or prior you the current date.

RunningMonth = 
Max( 0, 
    Datediff(
        Calculate( 
            Firstnonblank( table[date], if( [production], true(), blank ()) ),
            allexcept( table, table[well] )
        ),
        Table[date],
        Month
    ) + 1
)

This computes the first date with a production value greater than zero. It then takes the non-negative month difference between the current month and that month.

---EDIT-------------------------

To make this scalable, we can do the same thing with measures:

Production = IF( SUM ( table[water] ) = 0 || SUM ( table[oil] ) = 0 || SUM ( table[gas] ) = 0, TRUE(), BLANK() )

I updated production to evaluate to BLANK() instead of FALSE() for zero-production intersections to speed up evaluation later.

We will need a Calendar (Date) table linked to the production table for this next bit to work.

RunningActMonth = 
CALCULATE( 
    COUNTROWS( 
        FILTER( 
            DATESBETWEEN( &#39;Calendar&#39;[Date], BLANK(), LASTDATE( &#39;Calendar&#39;[Date] ) ),
            [Production]
        )
    ),
    ALLEXCEPT( table, table[well] )
)

RunningMonth = 
Max( 0, 
    DATEDIFF(
        CALCULATE( 
            FIRSTNONBLANK( &#39;Calendar&#39;[date], [Production] ),
            ALLEXCEPT( table, table[well] )
        ),
        LASTDATE( &#39;Calendar&#39;[Date] ),
        Month
    ) + 1
)

The only major changes here are referencing the Calendar table, which will speed up evaluation. Essentially, this will iterate over wells & dates. Your model speed will be determined by the number of wells X the number of dates in your Calendar.

huangapple
  • 本文由 发表于 2023年5月28日 08:22:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76349520.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定