Manipulating last two rows if there's data based on a Cut date

我的未来我决定 提交于 2019-12-11 06:06:56

问题


This question is a slightly varied version of this one...

Now I'm using Measures instead of Calculated columns and the date is static instead of having it based on a dropdown list.

Here's the Power BI test .pbix file:

https://drive.google.com/open?id=1OG7keqhdvDUDYkFQFMHyxcpi9Zi6Pn3d

This printscreen describes what I'm trying to accomplish:

Basically the date in P6 Update table is used as a cut date and will be fixed\static. It's imported from an Excel sheet where the user can customize it however they want.

Here's what should happen when a matching row in Test data table is found for P6 Update date:

  • column Earned Daily - must have its value summed with the next row if there's one;
  • column Earned Cum - must grab the next row's value;

  • all the previous rows should remain intact, that is, their values won't change;

  • all subsequent rows must have their values assigned 0.

So for example:

If P6 Update is 1-May-2018, this is the expected result:

1-May    7,498    52,106
2-May        0         0

If P6 Update is 30-Apr-2018, this is the expected result:

30-Apr  13,173    50,699
1-May        0         0
2-May        0         0 

If P6 Update is 29-Apr-2018, this is the expected result:

29-Apr  11,906    44,608
30-Apr       0         0
1-May        0         0
2-May        0         0 

and so on...

Hope this makes sense.

This is easier in Excel, but trying to do this in Power BI is making me go nuts.


回答1:


I will ignore previously asked related questions and start from scratch.

First, create a measure:

Current Earn = 
CALCULATE (
        SUM( 'Test data'[Value]),
        'Test data'[Act Rem] = "Actual Units",
        'Test data'[Type] = "Current"
    )

This measure will be used in other measures, to save you from typing all these conditions ("Actual Units" and "Current") again and again. It's a great practice to re-use measures in other measures - saves work, makes code cleaner and easier to refactor.

Create another measure:

Cut Date = SELECTEDVALUE('P6 Update'[Date])

We will use this measure whenever we need a cut off date. Please note that it does not have to be hard-coded - if P6 table contains a list of dates, you can create a pull-down slicer from the dates, and can choose the cut-off date dynamically. The formula will work properly.

Create third measure:

Next Earn =
VAR Cut_Date = [Cut Date]
VAR Current_Date = MAX ( 'Test data'[Date] )
VAR Next_Date = Current_Date + 1
VAR Current_Earn = [Current Earn]
VAR Next_Earn = CALCULATE ( [Current Earn], 'Test data'[Date] = Next_Date )
RETURN
    SWITCH (
        TRUE,
        Current_Date < Cut_Date, Current_Earn,
        Current_Date = Cut_Date, Current_Earn + Next_Earn,
        BLANK ()
    )

I am not sure if "Next Earn" is a good name for it, hopefully you will find a more intuitive name. The way it works: we save all necessary inputs into variables, and then use SWITCH function to define the results. Hopefully it's self-explanatory. (Note: if you need 0 above Cut Date, replace BLANK() with 0).

Finally, we define a measure for cumulative earn. It does not require any special logic, because previous measure takes care of it properly:

Cum Earn = 
VAR Current_Date = MAX('Test data'[Date])
RETURN
CALCULATE(
    [Next Earn],
    FILTER(ALL('Test data'[Date]), 'Test data'[Date] <= Current_Date))

Result:



来源:https://stackoverflow.com/questions/56677415/manipulating-last-two-rows-if-theres-data-based-on-a-cut-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!