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
kelvin-lkhca
Helper I
Helper I

Dax Formula for YTD Calculation

Dear All Expert,

 

I create a Dax formula to sum up the number of the periods selected to a YTD Revenue, EBIT and Margin. However, the YTD in the matrix table does not product the number. Can you please help to advise what goes wrong with the DAX formula?

 

I have shared the PBI file with the link below. Appreciate the solution from you in advance!

 

 

kelvinlkhca_0-1697726646929.png

 

kelvinlkhca_2-1697726947465.png

 

https://drive.google.com/file/d/1DfHcyslQ9DcRUlk5emR0LeM5bm-NlSmO/view?usp=sharing

5 REPLIES 5
Ashish_Mathur
Super User
Super User

Hi,

PBI file attached.

Ashish_Mathur_0-1697941268174.png

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hi Ashish Mathur,

 

Thanks for the proposed solution. Unfortunately, I can only present the Revenue (after elim), EBITxc1 and margin under 1 measure due to certain constraint. How do I get the YTD by tweaking the Dax formula in the original measure of my PBI file?

 

 

 

kelvinlkhca_1-1697989321538.png

 

 

 

kelvinlkhca_0-1697989205869.png

 

Hi @kelvin-lkhca 

 

Can you explain why the solution supplied by @Ashish_Mathur  doesn't work?  I can't see anything wrong.

 

If you have to "tweak" your measure, you'll have to fix a couple of things.

 

1) Your SWITCH statement is incorrect.  It never makes it down to the YTD calculations since it matches an earlier option.  I made a change to have all options check if Month is = OR <> to "YTD".

 

2) Your calculations for your _All variables are incorrect.  You can't use a variable like that in a CALCULATE statement.  Variables (actually constants) are only set once - not repeatedly like you are trying to recalculate it.  If you were using a measure like that, it would work.  (I hope that makes sense.)

 

I made those changes to your measure but still have a problem with the _All calulations.  (I probably won't be able to do much more until tomorrow.)

 

Let me know if the above doesn't make sense.

 

 

Measure = 
VAR EBITxc1 =
    CALCULATE(
        [M_Sum],
        Fact_Data[Account] = "EBIT"
    )
VAR Revenue_Elim =
    CALCULATE(
        [M_Sum],
        Fact_Data[Account] = "Revenue"
    )
VAR EBITxc1Margin =
    FORMAT(
        DIVIDE(
            EBITxc1,
            Revenue_Elim
        ),
        "#,##.0%;-#,##.0%"
    )
VAR EBITxc1_All = // Changed but still not working **********************
    CALCULATE(
        [M_Sum],
        Fact_Data[Account] = "EBIT",
        ALLSELECTED( Period_YTD_Display[Month] )
    )
VAR Revenue_Elim_All = // Changed but still not working **********************
    CALCULATE(
        [M_Sum],
        Fact_Data[Account] = "Revenue",
        ALLSELECTED( Period_YTD_Display[Month] )
    )
VAR EBITxc1Margin_All =
	FORMAT(
		CALCULATE(
			DIVIDE(
			    CALCULATE(
                    [M_Sum],
                    Fact_Data[Account] = "EBIT"
                ),
			    CALCULATE(
                    [M_Sum],
                    Fact_Data[Account] = "Revenue"
                )
			),
			ALLSELECTED( Period_YTD_Display[Month] )
		),
		"#,##.0%;-#,##.0%"
	)
VAR Account = SELECTEDVALUE( Account_Display_Seq[Account Description] )
VAR Period = SELECTEDVALUE( Period_YTD_Display[Month] )
VAR Output =
    SWITCH(
        TRUE(),
        Account = "EBITxc1 margin"
            && Period <> "YTD", 
            EBITxc1Margin,
        Account = "EBITxc1"
            && Period <> "YTD", 
            FORMAT( EBITxc1, "#,###;-#,###" ),
        Account = "Revenue (after elim)"
            && Period <> "YTD", 
            FORMAT( Revenue_Elim, "#,###;-#,###" ),
        Account = "EBITxc1 margin"
            && Period = "YTD", 
            EBITxc1Margin_All,
        Account = "EBITxc1"
            && Period = "YTD",
            FORMAT( EBITxc1_All, "#,###;-#,###" ),
        Account = "Revenue (after elim)"
            && Period = "YTD",
            FORMAT( Revenue_Elim_All, "#,###;-#,###" )
    )
RETURN
    Output

 

 

 

 

 

 

 

 

 

Hi Gmsamborn,

 

@gmsamborn 

This is because in my real data, I will have to put up a slicer which allow user to choose the account to show as below. By using the solution provided by @Ashish_Mathur . The account which are filtered out will still be remained in the matrix table because they are from different measure. Whereas by using the concept of my measure(although the formula is not correct), only the account being selected will be shown in the matrix table. 

How my explanation above can justify why the proposed solution by @Ashish_Mathur  is good but still not fully solve the problem I am facing.

 

 

 

kelvinlkhca_0-1698066027819.png

 

lbendlin
Super User
Super User

Please use the "Quick Measures"  feature in Power BI. It includes an example for YTD calculation.

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.

RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

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