cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
ningty09 Regular Visitor
Regular Visitor

Power BI Not Comparing Date in a Measure Properly to Date in a Data Table

Hi all,
 

 

I'm trying to compare a date measure with a date in my data table.  The date measure I have is:

 

IF(CALCULATE(SUM(YieldDataTable[RunQuantity]), FILTER(YieldDataTable,YieldDataTable[RunDate].[Date]=TODAY()))>100, TODAY(),
IF(CALCULATE(SUM(YieldDataTable[RunQuantity]), FILTER(YieldDataTable,YieldDataTable[RunDate].[Date]=TODAY()-1))>100, TODAY()-1,
IF(CALCULATE(SUM(YieldDataTable[RunQuantity]), FILTER(YieldDataTable,YieldDataTable[RunDate].[Date]=TODAY()-2))>100, TODAY()-2,
IF(CALCULATE(SUM(YieldDataTable[RunQuantity]), FILTER(YieldDataTable,YieldDataTable[RunDate].[Date]=TODAY()-3))>100, TODAY()-4,
BLANK())))

 

Which works perfectly and does exactly what I want it to do.  Below is the output it has:

1.PNG

However, when I try to use this date, it doesn't work any more (It returns a blank): 

 

Day Before Last Production Date = 
SWITCH (
    TRUE (),
    CALCULATE (
        SUM ( YieldDataTable[RunQuantity] ),
        FILTER ( YieldDataTable, YieldDataTable[RunDate].[Date] = [Last Production Date] - 1 )
    ) > 100, [Last Production Date]-1,
    CALCULATE (
        SUM ( YieldDataTable[RunQuantity] ),
        FILTER ( YieldDataTable, YieldDataTable[RunDate].[Date] = [Last Production Date] - 2 )
    ) > 100, [Last Production Date] - 2,
    CALCULATE (
        SUM ( YieldDataTable[RunQuantity] ),
        FILTER ( YieldDataTable, YieldDataTable[RunDate].[Date] = [Last Production Date] - 3 )
    ) > 100, [Last Production Date] - 3,
    CALCULATE (
        SUM ( YieldDataTable[RunQuantity] ),
        FILTER ( YieldDataTable, YieldDataTable[RunDate].[Date] = [Last Production Date] - 4 )
    ) > 100, [Last Production Date] - 4,
    BLANK ()
)
Last production date is formated as a date (M/d/yyyy), same with my data table. 
A sample of my data table date is below:
2.PNG

Any help is appreciated!

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Super User
Super User

Re: Power BI Not Comparing Date in a Measure Properly to Date in a Data Table

Hi @ningty09 

I haven't looked in detail but context transition might very well be having an effect in your measure. Bear in mind you are invoking it within FILTER, where there's a row context. Try:

Day Before Last Production Date = 
VAR LastProd_ = [Last Production Date]  //invoke the measure where there's no row context
SWITCH ( TRUE (), CALCULATE ( SUM ( YieldDataTable[RunQuantity] ), FILTER ( YieldDataTable, YieldDataTable[RunDate].[Date] = LastProd_ - 1 ) ) > 100, [Last Production Date]-1, CALCULATE ( SUM ( YieldDataTable[RunQuantity] ), FILTER ( YieldDataTable, YieldDataTable[RunDate].[Date] = LastProd_ - 2 ) ) > 100, [Last Production Date] - 2, CALCULATE ( SUM ( YieldDataTable[RunQuantity] ), FILTER ( YieldDataTable, YieldDataTable[RunDate].[Date] = LastProd_ - 3 ) ) > 100, [Last Production Date] - 3, CALCULATE ( SUM ( YieldDataTable[RunQuantity] ), FILTER ( YieldDataTable, YieldDataTable[RunDate].[Date] = LastProd_ - 4 ) ) > 100, [Last Production Date] - 4, BLANK () )

 

Please mark the question solved when done and consider giving kudos if posts are helpful.

Cheers  Datanaut

View solution in original post

4 REPLIES 4
Highlighted
Super User
Super User

Re: Power BI Not Comparing Date in a Measure Properly to Date in a Data Table

Hi @ningty09 

I haven't looked in detail but context transition might very well be having an effect in your measure. Bear in mind you are invoking it within FILTER, where there's a row context. Try:

Day Before Last Production Date = 
VAR LastProd_ = [Last Production Date]  //invoke the measure where there's no row context
SWITCH ( TRUE (), CALCULATE ( SUM ( YieldDataTable[RunQuantity] ), FILTER ( YieldDataTable, YieldDataTable[RunDate].[Date] = LastProd_ - 1 ) ) > 100, [Last Production Date]-1, CALCULATE ( SUM ( YieldDataTable[RunQuantity] ), FILTER ( YieldDataTable, YieldDataTable[RunDate].[Date] = LastProd_ - 2 ) ) > 100, [Last Production Date] - 2, CALCULATE ( SUM ( YieldDataTable[RunQuantity] ), FILTER ( YieldDataTable, YieldDataTable[RunDate].[Date] = LastProd_ - 3 ) ) > 100, [Last Production Date] - 3, CALCULATE ( SUM ( YieldDataTable[RunQuantity] ), FILTER ( YieldDataTable, YieldDataTable[RunDate].[Date] = LastProd_ - 4 ) ) > 100, [Last Production Date] - 4, BLANK () )

 

Please mark the question solved when done and consider giving kudos if posts are helpful.

Cheers  Datanaut

View solution in original post

ningty09 Regular Visitor
Regular Visitor

Re: Power BI Not Comparing Date in a Measure Properly to Date in a Data Table

That worked!! Thank you so much!! Can you please explain why this makes it work?

Super User
Super User

Re: Power BI Not Comparing Date in a Measure Properly to Date in a Data Table

Are you familiar with context transition? If not, check this out:  https://www.sqlbi.com/articles/understanding-context-transition/

If you execute a measure inside a FILTER() the way you were doing:

FILTER ( YieldDataTable, YieldDataTable[RunDate].[Date] = [Last Production Date] - 1 )

FILTER iterates over each row of YieldDataTable and, for each one, calculates [Last Production Date]. Now, since we have row context, context transition kicks in when the measure is invoked and that means you have a filter in each of the columns. That obviously has an effect in the result of the measure. From what you showed above, you want [Last Production Date] to be calculated with no filters applied (or at least not with the ones deriving from context transition). If we invoke the measure at the beginning:

VAR LastProd_ = [Last Production Date]  //invoke the measure where there's no row context

we have no row context there, so we save the result in a VAR (immutable content) and used it subsequently as many times as necessary. Note that by using the VAR we are also more efficient since we invoke the measure only once at the beginning rather once for every row in the FILTER operations.

 

Please mark the question solved when done and consider giving kudos if posts are helpful.

Cheers  Datanaut

 

ningty09 Regular Visitor
Regular Visitor

Re: Power BI Not Comparing Date in a Measure Properly to Date in a Data Table

I read through the link you sent and that made sense.  Thank you!

Helpful resources

Announcements
Ask Amanda Anything Q&A

Ask Amanda Anything Q&A

Learn the answers to some of the questions asked during the Amanda Triple A event.

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.

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: 269 members 2,413 guests
Please welcome our newest community members: