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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
Joao_Arienti
New Member

YTD from a measure or another depending on having data or not

Hi everyone,

 

I need your help on how to calculate YTD from two measures depending if one of them has data or not. And if not, it should be replaced by the other one.

 

At first, it seemed easy, but I'm having a hard time solving it. The concept is Measure BBB should be used and when Measure BBB has zero as value, it should be replaced by Measure AAA monthly inside a YTD calculation.

 

I have a dim measure table like this:

Measure SKMeasure Name
1AAA
2BBB
3CCC
4DDD
5EEE

 

And a fact table like this:

Year MonthDate SKProduct SKStore SKMeasure SKMeasure Value
2020012020010111111110
20200120200101111111210
2020012020010111111135
202002202002011111111100
2020022020020111111120
2020022020020111111135
2020032020030111111111000
2020032020030111111122000
2020032020030111111135

 

And I'd like the result to be something like this:

Year MonthProduct SKStore SKMeasure SK AAAMeasure SK BBBMeasure SK CCCOutputYTD Output
20200111111101051010
20200211111110005100110
20200311111110002000520002110

 

I leave here a link to a PowerBI example file with the data.

Link to PowerBI file 

 

Using DAX Studio, I tried a lot of different things, but I keep getting it wrong:

DEFINE
    MEASURE 'Fact_Table'[test_measure] =
        VAR _m1 =
            CALCULATE (
                SUM ( 'Fact_Table'[Measure Value] ),
                'Dim_Measure'[Measure Name] = "BBB"
            )
        VAR _m2 =
            CALCULATE (
                SUM ( 'Fact_Table'[Measure Value] ),
                'Dim_Measure'[Measure Name] = "AAA"
            )
        RETURN
            IF ( _m1 = 0, _m2, _m1 )
    MEASURE 'Fact_Table'[_m1] =
        CALCULATE (
            SUM ( 'Fact_Table'[Measure Value] ),
            'Dim_Measure'[Measure Name] = "BBB"
        )
    MEASURE 'Fact_Table'[_m2] =
        CALCULATE (
            SUM ( 'Fact_Table'[Measure Value] ),
            'Dim_Measure'[Measure Name] = "AAA"
        )
    VAR _y =
        TREATAS ( { "2020" }, 'Date'[Year] )
    VAR _mn =
        TREATAS ( { "AAA", "BBB" }, 'Dim_Measure'[Measure Name] )


EVALUATE
    SUMMARIZECOLUMNS (
        'Date'[Year Month],
        _y,
        "m1", 'Fact_Table'[_m1],
        "m2", 'Fact_Table'[_m2],
        "new_test", 'Fact_Table'[test_measure],
        "new_test_ytd", TOTALYTD ( 'Fact_Table'[new_test], 'Date'[Date] ),
        "new_test_ytd_new", CALCULATE ( 'Fact_Table'[new_test], DATESYTD ( 'Date'[Date] ) )
    )
    ORDER BY 'Date'[Year Month]

 

If anyone could help me, I'd be very grateful.

1 ACCEPTED SOLUTION
v-yiruan-msft
Community Support
Community Support

Hello @Joao_Arienti ,

Update the [Output] measurement formula as follows:

Output = SUMX(VALUES('Fact_Table'[Year Month]),IF( [Total Measure BBB] = 0, [Total Measure AAA], [Total Measure BBB] ) )​

YTD from a measure or another depending on having data or not.JPG

Best regards

Rena

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

4 REPLIES 4
v-yiruan-msft
Community Support
Community Support

Hello @Joao_Arienti ,

Update the [Output] measurement formula as follows:

Output = SUMX(VALUES('Fact_Table'[Year Month]),IF( [Total Measure BBB] = 0, [Total Measure AAA], [Total Measure BBB] ) )​

YTD from a measure or another depending on having data or not.JPG

Best regards

Rena

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thank you very much, Rena. Your suggestion seems to do the trick.

amitchandak
Super User
Super User

@Joao_Arienti , I have not checked the power bi file yet.

The first thing I noticed, this should

IF ( coalesce(_m1,0) = 0, _m2, _m1 )

or

IF ( isblank(_m1,0), _m1 , _m2)

lbendlin
Super User
Super User

Since you want your measure BBB to replace the measure AAA on a monthly basis you need to iterate through all "rows"  in your filter context. 

 

I would use a SELECTCOLUMNS() statement to create a table variable with the month name and the AAA and BBB measure for that month.  In the next step I would then use ADDCOLUMNS to decide which of the measures to use, and as a last step I would then add the YTD computation for each row of the temp table.

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

Power BI Carousel June 2024

Power BI Monthly Update - June 2024

Check out the June 2024 Power BI update to learn about new features.

RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.