cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Iamnvt Member
Member

Find Previous Value for a measure with multiple conditions

hi,

 

I have a model with 2 facts table: Plan and Actual

Four dimension tables: Date, Hour, Product, Line

 

Model.PNG

 

I need to write a measure (not column) to show the previous value of HC, where for the same line, same product, any date, any hour, if the Plan is blank(), or Plan is 0, and Actual >0, then Previous HC = the most recent HC, where Plan >0

Like this:

 

Model2.PNG

 

here is the PBI file: link

I wrote a measure: 

VAR
__Filtertable  = FILTER(ADDCOLUMNS(CROSSJOIN('Date', Line, 'Product', 'Hour'), "Act", [Sum of Actual], "Pl", [Sum of Plan], "HC", [Sum of HC]),
[Act] + [Pl] <> BLANK())
VAR
__Table1 = ADDCOLUMNS(__Filtertable,
"Previous HC",
  VAR
        __currentdate = FIRSTNONBLANK('Date'[Date],1)
  VAR
        __currentline = FIRSTNONBLANK(Line[Line], 1)
  VAR
        __currentproduct = FIRSTNONBLANK('Product'[Product],1)
  VAR
        __previousdate = CALCULATE(LASTNONBLANK('Date'[Date],1), FILTER(__Filtertable, 'Date'[Date] < __currentdate && Line[Line] = __currentline && 'Product'[Product] = __currentproduct))
   RETURN
        CALCULATE([Sum of HC], FILTER(__Filtertable, Line[Line] = __currentline && 'Product'[Product] = __currentproduct && 'Date'[Date] = __previousdate)
))
RETURN
SUMX(FILTER(__Table1, [Act] > 0 && [Pl] = 0), [Previous HC])

but it doesn't give the result.

please help on this.

Thanks very much

@OwenAuger  : please take a look

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Super User
Super User

Re: Find Previous Value for a measure with multiple conditions

Try this, although I believe it's overly complex due to the structure you are using (swap the ";" for "," if necessary)

Sum of Previous HC =
IF (
    [Sum of Actual] > 0 && ( ISBLANK ( [Sum of Plan] ) || [Sum of Plan] = 0);
    VAR AuxTable_ =
        ADDCOLUMNS (
            CROSSJOIN (
                DISTINCT ( Line[Line] );
                CALCULATETABLE (
                    DISTINCT ( 'Date'[Date] );
                    FILTER ( ALL ( 'Date'[Date] ); 'Date'[Date] < SELECTEDVALUE ( 'Date'[Date] ) )
                );
                DISTINCT ( 'Product'[Product] );
                ALL ( 'Hour'[Hour] )
            );
            "AuxPlan_"; [Sum of Plan];
            "AuxHC_"; [Sum of HC];
            "SumActual_"; [Sum of Actual]
        )
    RETURN
        SUMX ( AuxTable_; [AuxHC_] )
)

 

View solution in original post

4 REPLIES 4
Super User
Super User

Re: Find Previous Value for a measure with multiple conditions

Hi @Iamnvt 

why is the second value hand-written in blue 5 and not 10? You're not considering the current date as valid for the "most recent value"? 

Iamnvt Member
Member

Re: Find Previous Value for a measure with multiple conditions

Because I am looking the most recent value of line A, Product Apple, where it has Plan > 0 of previous date —> which is the HC value in row#1
Super User
Super User

Re: Find Previous Value for a measure with multiple conditions

Try this, although I believe it's overly complex due to the structure you are using (swap the ";" for "," if necessary)

Sum of Previous HC =
IF (
    [Sum of Actual] > 0 && ( ISBLANK ( [Sum of Plan] ) || [Sum of Plan] = 0);
    VAR AuxTable_ =
        ADDCOLUMNS (
            CROSSJOIN (
                DISTINCT ( Line[Line] );
                CALCULATETABLE (
                    DISTINCT ( 'Date'[Date] );
                    FILTER ( ALL ( 'Date'[Date] ); 'Date'[Date] < SELECTEDVALUE ( 'Date'[Date] ) )
                );
                DISTINCT ( 'Product'[Product] );
                ALL ( 'Hour'[Hour] )
            );
            "AuxPlan_"; [Sum of Plan];
            "AuxHC_"; [Sum of HC];
            "SumActual_"; [Sum of Actual]
        )
    RETURN
        SUMX ( AuxTable_; [AuxHC_] )
)

 

View solution in original post

Iamnvt Member
Member

Re: Find Previous Value for a measure with multiple conditions

@AlB  this looks ok, but how can I make the Grand Total correct as sum of the row?

Helpful resources

Announcements
October 2019 Community Highlights

October 2019 Community Highlights

October was a busy month in the community. Read the recap article to learn about some of the events and content.

New Solution Badges

New Solution Badges

Two waves of brand new solution badges are coming! Read the article for more information on our new community badges.

Ask Amir Anything

Exclusive LIVE Community Event No. 2 – Ask Amir Anything

Next in our Triple A series: Ask Amir Netz questions about the latest updates, features and future.

Analytics in Azure virtual event

Analytics in Azure virtual event

Experience a limitless analytics service built to ingest, prep, manage, and serve data for immediate use in Power BI.

Users Online
Currently online: 225 members 2,226 guests
Please welcome our newest community members: