Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get certified as a Fabric Data Engineer: Check your eligibility for a 50% exam voucher offer and join us for free live learning sessions to get prepared for Exam DP-700. Get started

Reply
Erwin
Helper II
Helper II

Pls explain why why this works with measures but not with variables

Hi all,

Please review below data model.

Capture.PNG

Based on this model I have created the following measures.

NumOfSickCasesReported = 
COUNTROWS ( 
     SickCases
)
NumOfSickCasesOpen = 
CALCULATE (
     [NumOfSickCasesReported],
     FILTER ( 
         ALL ( SickCases[RecoveryDate] ),
         SickCases[RecoveryDate] > MAX ( Calendar12M[Date] )
     )
)
RTOfSickCasesOpen = 
CALCULATE ( 
    [NumOfSickCasesOpen],
    FILTER (
        ALL( Calendar12M[Date] ),
        Calendar12M[Date] <= MAX ( Calendar12M[Date] ) 
    )
)

The RTOfSickCasesOpen measure gives me exactly what I need, a running total number of sick cases open per reporting date, regardless of when the first sick day occurred. Both other measure are just intermediate measures I'm not going to need when creating the dashboard.

 

A cleaner option would be to create just one measure RTOfSickCasesOpen, with the other intermediate measures as variables.

RTOfSickCasesOpen = 
VAR NumOfSickCasesReported = 
COUNTROWS ( 
     SickCases
)

VAR NumOfSickCasesOpen = 
CALCULATE (
     NumOfSickCasesReported,
     FILTER ( 
         ALL ( SickCases[RecoveryDate] ),
         SickCases[RecoveryDate] > MAX ( Calendar12M[Date] )
     )
)

VAR RTOfSickCasesOpen = 
CALCULATE ( 
    NumOfSickCasesOpen,
    FILTER (
        ALL( Calendar12M[Date] ),
        Calendar12M[Date] <= MAX ( Calendar12M[Date] ) 
    )
)

RETURN
RTOfSickCasesOpen

However, this cleaner version of RTOfSickCasesOpen doesn't seem to work as expected.

 

Can anyone tell me why this is the case?

 

Regards,

Erwin

 

 

1 ACCEPTED SOLUTION
MFelix
Super User
Super User

Hi @Erwin ,

 

This as to do with with the context and filter transition within the calculates.

 

Check this blog post by @marcorusso .

 

Believe that you need to redo your measure having the variable within each calculate rather than putting it all before the syntax of the full calculation would be something like this:

 

RTOfSickCasesOpen =
VAR NumOfSickCasesReported =
    COUNTROWS ( SickCases )
RETURN
    CALCULATE (
        VAR NumOfSickCasesOpen =
            CALCULATE (
                NumOfSickCasesReported,
                FILTER (
                    ALL ( SickCases[RecoveryDate] ),
                    SickCases[RecoveryDate] > MAX ( Calendar12M[Date] )
                )
            )
        RETURN
            NumOfSickCasesOpen,
        FILTER (
            ALL ( Calendar12M[Date] ),
            Calendar12M[Date] <= MAX ( Calendar12M[Date] )
        )
    )

Don't have the data to match it but you need to place your VAR within the several measures and not all on top of the expected calculation.

 

@marcorusso  can you please confirm/infirm my suggestion.

 

Regards,

MFelix

 

 


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



View solution in original post

1 REPLY 1
MFelix
Super User
Super User

Hi @Erwin ,

 

This as to do with with the context and filter transition within the calculates.

 

Check this blog post by @marcorusso .

 

Believe that you need to redo your measure having the variable within each calculate rather than putting it all before the syntax of the full calculation would be something like this:

 

RTOfSickCasesOpen =
VAR NumOfSickCasesReported =
    COUNTROWS ( SickCases )
RETURN
    CALCULATE (
        VAR NumOfSickCasesOpen =
            CALCULATE (
                NumOfSickCasesReported,
                FILTER (
                    ALL ( SickCases[RecoveryDate] ),
                    SickCases[RecoveryDate] > MAX ( Calendar12M[Date] )
                )
            )
        RETURN
            NumOfSickCasesOpen,
        FILTER (
            ALL ( Calendar12M[Date] ),
            Calendar12M[Date] <= MAX ( Calendar12M[Date] )
        )
    )

Don't have the data to match it but you need to place your VAR within the several measures and not all on top of the expected calculation.

 

@marcorusso  can you please confirm/infirm my suggestion.

 

Regards,

MFelix

 

 


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Prices go up Feb. 11th.

Feb2025 Sticker Challenge

Join our Community Sticker Challenge 2025

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

Jan NL Carousel

Fabric Community Update - January 2025

Find out what's new and trending in the Fabric community.