cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
sufiyanakhtar
Helper I
Helper I

Help creating Dax Measure

Dear Power Users,

I have a model as shown below. I am also having a disconnected table which is acting as slicers for my value (MTD,QTD,YTD). I want to calculate a measure based on the slicers. I am currently using this formula:

 

Measure = 

SWITCH(TRUE(),
SELECTEDVALUE(Slicer Value) = "MTD", CALCULATE(SUM('CORPDW TRIALBALANCE'[Amount_in_Transaction_Currency]), DATESBETWEEN('CORPDW FISCALYEARROLLINGPERIOD'[Day], MIN('CORPDW FISCALYEARROLLINGPERIOD'[MTD_START_DATE]), MAX('CORPDW FISCALYEARROLLINGPERIOD'[MTD_END_DATE])) ,'CORPDW ACCOUNT'[AccType] = "Expense"),

SELECTEDVALUE('Period Slicers'[Period Slicer]) = "QTD", CALCULATE(SUM('CORPDW TRIALBALANCE'[Amount_in_Transaction_Currency]), DATESBETWEEN('CORPDW FISCALYEARROLLINGPERIOD'[Day], MIN('CORPDW FISCALYEARROLLINGPERIOD'[QTD_START_DATE]), MAX('CORPDW FISCALYEARROLLINGPERIOD'[QTD_END_DATE])) ,'CORPDW ACCOUNT'[AccType] = "Expense"),

SELECTEDVALUE('Period Slicers'[Period Slicer]) = "YTD", CALCULATE(SUM('CORPDW TRIALBALANCE'[Amount_in_Transaction_Currency]), DATESBETWEEN('CORPDW FISCALYEARROLLINGPERIOD'[Day], MIN('CORPDW FISCALYEARROLLINGPERIOD'[YTD_START_DATE]), MAX('CORPDW FISCALYEARROLLINGPERIOD'[YTD_END_DATE])) ,'CORPDW ACCOUNT'[AccType] = "Expense"),


""
)

 

 

The issue is that no matter I select MTD,QTD OR YTD, it shows me the same value. Please help!!!

1 ACCEPTED SOLUTION
SpartaBI
Community Champion
Community Champion

@sufiyanakhtar hey, you implemented the logic not in the classic way so hard to say what is going on.
You should go read and be familiar with the best practice tecniques for these time intelligence patterns:
https://www.daxpatterns.com/time-patterns/

Another important question: I see you used the column  'CORPDW FISCALYEARROLLINGPERIOD'[Day]
in your measures. Is that a data or a day number between 1 to 31?
It needs to be a date, maybe that is all the issue.
I anyway cleaned up your code a little and also added REMOVEFILTERS just to be sure but in case it's a 
'CORPDW FISCALYEARROLLINGPERIOD'[Day] date column than it's not neccesary. So, this is the most important question, what is that column.

 

Measure1 =
VAR _slicr_value =
    SELECTEDVALUE ( 'Period Slicers'[Period Slicer] )
RETURN
    SWITCH (
        _slicr_value,
        "MTD",
        VAR _mtd_start_date = MIN ( 'CORPDW FISCALYEARROLLINGPERIOD'[MTD_START_DATE] )
        VAR _mtd_end_date = MAX ( 'CORPDW FISCALYEARROLLINGPERIOD'[MTD_END_DATE] )
        RETURN
            CALCULATE (
                SUM ( 'CORPDW TRIALBALANCE'[Amount_in_Transaction_Currency] ),
                DATESBETWEEN (
                    'CORPDW FISCALYEARROLLINGPERIOD'[Day],
                    _mtd_start_date,
                    _mtd_end_date
                ),
                REMOVEFILTERS('CORPDW FISCALYEARROLLINGPERIOD'),
                'CORPDW ACCOUNT'[AccType] = "Expense"
            ),
        "QTD",
        VAR _qtd_start_date = MIN ( 'CORPDW FISCALYEARROLLINGPERIOD'[QTD_START_DATE] )
        VAR _qtd_end_date = MAX ( 'CORPDW FISCALYEARROLLINGPERIOD'[QTD_END_DATE] )
        RETURN    
            CALCULATE (
                SUM ( 'CORPDW TRIALBALANCE'[Amount_in_Transaction_Currency] ),
                DATESBETWEEN (
                    'CORPDW FISCALYEARROLLINGPERIOD'[Day],
                    _qtd_start_date,
                    _qtd_end_date
                ),
                REMOVEFILTERS('CORPDW FISCALYEARROLLINGPERIOD'),
                'CORPDW ACCOUNT'[AccType] = "Expense"
            ),
        "YTD",
        VAR _ytd_start_date = MIN ( 'CORPDW FISCALYEARROLLINGPERIOD'[YTD_START_DATE] )
        VAR _ytd_end_date = MAX ( 'CORPDW FISCALYEARROLLINGPERIOD'[YTD_END_DATE] )
        RETURN
            CALCULATE (
                SUM ( 'CORPDW TRIALBALANCE'[Amount_in_Transaction_Currency] ),
                DATESBETWEEN (
                    'CORPDW FISCALYEARROLLINGPERIOD'[Day],
                    _ytd_start_date,
                    _ytd_end_date
                ),
                REMOVEFILTERS('CORPDW FISCALYEARROLLINGPERIOD'),
                'CORPDW ACCOUNT'[AccType] = "Expense"
            )
    )

 

 


2022-05-19 17_30_22-Re_ Need help on DAX function with measure vs colu... - Microsoft Power BI Commu.png

Showcase Report – Contoso By SpartaBI


SpartaBI_3-1652115470761.png   SpartaBI_1-1652115142093.png   SpartaBI_2-1652115154505.png

Full-Logo11.png

View solution in original post

11 REPLIES 11
SpartaBI
Community Champion
Community Champion

@sufiyanakhtar hey, you implemented the logic not in the classic way so hard to say what is going on.
You should go read and be familiar with the best practice tecniques for these time intelligence patterns:
https://www.daxpatterns.com/time-patterns/

Another important question: I see you used the column  'CORPDW FISCALYEARROLLINGPERIOD'[Day]
in your measures. Is that a data or a day number between 1 to 31?
It needs to be a date, maybe that is all the issue.
I anyway cleaned up your code a little and also added REMOVEFILTERS just to be sure but in case it's a 
'CORPDW FISCALYEARROLLINGPERIOD'[Day] date column than it's not neccesary. So, this is the most important question, what is that column.

 

Measure1 =
VAR _slicr_value =
    SELECTEDVALUE ( 'Period Slicers'[Period Slicer] )
RETURN
    SWITCH (
        _slicr_value,
        "MTD",
        VAR _mtd_start_date = MIN ( 'CORPDW FISCALYEARROLLINGPERIOD'[MTD_START_DATE] )
        VAR _mtd_end_date = MAX ( 'CORPDW FISCALYEARROLLINGPERIOD'[MTD_END_DATE] )
        RETURN
            CALCULATE (
                SUM ( 'CORPDW TRIALBALANCE'[Amount_in_Transaction_Currency] ),
                DATESBETWEEN (
                    'CORPDW FISCALYEARROLLINGPERIOD'[Day],
                    _mtd_start_date,
                    _mtd_end_date
                ),
                REMOVEFILTERS('CORPDW FISCALYEARROLLINGPERIOD'),
                'CORPDW ACCOUNT'[AccType] = "Expense"
            ),
        "QTD",
        VAR _qtd_start_date = MIN ( 'CORPDW FISCALYEARROLLINGPERIOD'[QTD_START_DATE] )
        VAR _qtd_end_date = MAX ( 'CORPDW FISCALYEARROLLINGPERIOD'[QTD_END_DATE] )
        RETURN    
            CALCULATE (
                SUM ( 'CORPDW TRIALBALANCE'[Amount_in_Transaction_Currency] ),
                DATESBETWEEN (
                    'CORPDW FISCALYEARROLLINGPERIOD'[Day],
                    _qtd_start_date,
                    _qtd_end_date
                ),
                REMOVEFILTERS('CORPDW FISCALYEARROLLINGPERIOD'),
                'CORPDW ACCOUNT'[AccType] = "Expense"
            ),
        "YTD",
        VAR _ytd_start_date = MIN ( 'CORPDW FISCALYEARROLLINGPERIOD'[YTD_START_DATE] )
        VAR _ytd_end_date = MAX ( 'CORPDW FISCALYEARROLLINGPERIOD'[YTD_END_DATE] )
        RETURN
            CALCULATE (
                SUM ( 'CORPDW TRIALBALANCE'[Amount_in_Transaction_Currency] ),
                DATESBETWEEN (
                    'CORPDW FISCALYEARROLLINGPERIOD'[Day],
                    _ytd_start_date,
                    _ytd_end_date
                ),
                REMOVEFILTERS('CORPDW FISCALYEARROLLINGPERIOD'),
                'CORPDW ACCOUNT'[AccType] = "Expense"
            )
    )

 

 


2022-05-19 17_30_22-Re_ Need help on DAX function with measure vs colu... - Microsoft Power BI Commu.png

Showcase Report – Contoso By SpartaBI


SpartaBI_3-1652115470761.png   SpartaBI_1-1652115142093.png   SpartaBI_2-1652115154505.png

Full-Logo11.png

Hey Sparta, The day column is a date type column. Actually my Data architect has designed the backend because the data is aggregated on monthly basis. So the day you are referring to has dates like 01/01/2020, 02/01/2020, 03/01/2020 (MM/DD/YYYY) and so on.... Please help as IVE BEEN BREAKING MY HEAD ON THIS 💔

@sufiyanakhtar 
Wait, you have a proper data table? Continous and unique dates or the data table is on a monthly granularity?
P.S.  Did you check my measure anyway?

 

Dude, your measure worked!!! Let me just verify the values with the SQL server values and then Ill mark your reply as solution!!! Also, if you could please explain to me what I was doing wrong, itd be really helpful!

@sufiyanakhtar my pleasure 🙂
I have some suspects 🙂 It's late here so we'll continue tomorrow. 

P.S: check out my showcase report:
https://community.powerbi.com/t5/Data-Stories-Gallery/SpartaBI-Feat-Contoso-100K/td-p/2449543
Give it a thumbs up if you liked it 🙂

Sure Sparta, but please do let me know for me to understand this better 🙂

@sufiyanakhtar so as I said, I have a suspect, but first need to do some testing to verify my guess.
Run this measure and tell me if you still get the right result or the same as the original wrong result:

 

Measure1 =
VAR _slicr_value =
    SELECTEDVALUE ( 'Period Slicers'[Period Slicer] )
RETURN
    SWITCH (
        _slicr_value,
        "MTD",
        VAR _mtd_start_date = MIN ( 'CORPDW FISCALYEARROLLINGPERIOD'[MTD_START_DATE] )
        VAR _mtd_end_date = MAX ( 'CORPDW FISCALYEARROLLINGPERIOD'[MTD_END_DATE] )
        RETURN
            CALCULATE (
                SUM ( 'CORPDW TRIALBALANCE'[Amount_in_Transaction_Currency] ),
                DATESBETWEEN (
                    'CORPDW FISCALYEARROLLINGPERIOD'[Day],
                    _mtd_start_date,
                    _mtd_end_date
                ),
                'CORPDW ACCOUNT'[AccType] = "Expense"
            ),
        "QTD",
        VAR _qtd_start_date = MIN ( 'CORPDW FISCALYEARROLLINGPERIOD'[QTD_START_DATE] )
        VAR _qtd_end_date = MAX ( 'CORPDW FISCALYEARROLLINGPERIOD'[QTD_END_DATE] )
        RETURN    
            CALCULATE (
                SUM ( 'CORPDW TRIALBALANCE'[Amount_in_Transaction_Currency] ),
                DATESBETWEEN (
                    'CORPDW FISCALYEARROLLINGPERIOD'[Day],
                    _qtd_start_date,
                    _qtd_end_date
                ),
                'CORPDW ACCOUNT'[AccType] = "Expense"
            ),
        "YTD",
        VAR _ytd_start_date = MIN ( 'CORPDW FISCALYEARROLLINGPERIOD'[YTD_START_DATE] )
        VAR _ytd_end_date = MAX ( 'CORPDW FISCALYEARROLLINGPERIOD'[YTD_END_DATE] )
        RETURN
            CALCULATE (
                SUM ( 'CORPDW TRIALBALANCE'[Amount_in_Transaction_Currency] ),
                DATESBETWEEN (
                    'CORPDW FISCALYEARROLLINGPERIOD'[Day],
                    _ytd_start_date,
                    _ytd_end_date
                ),
                'CORPDW ACCOUNT'[AccType] = "Expense"
            )
    )

 


Also, don't forget to mark the message with the measure that worked as a solution for community visabilty



Hey Sparta,

Both of them yield the same result which is shocking tbh because I tried the same formula as your most recent one and it didnt work before. Anyways, both of them yield the same output

@sufiyanakhtar it is strange.
So you are saying that both worked but this (your original) didn't:

 

Measure1 =
SWITCH (
    TRUE (),
    SELECTEDVALUE ( 'Period Slicers'[Period Slicer] ) = "MTD",
        CALCULATE (
            SUM ( 'CORPDW TRIALBALANCE'[Amount_in_Transaction_Currency] ),
            DATESBETWEEN (
                'CORPDW FISCALYEARROLLINGPERIOD'[Day],
                MIN ( 'CORPDW FISCALYEARROLLINGPERIOD'[MTD_START_DATE] ),
                MAX ( 'CORPDW FISCALYEARROLLINGPERIOD'[MTD_END_DATE] )
            ),
            'CORPDW ACCOUNT'[AccType] = "Expense"
        ),
    SELECTEDVALUE ( 'Period Slicers'[Period Slicer] ) = "QTD",
        CALCULATE (
            SUM ( 'CORPDW TRIALBALANCE'[Amount_in_Transaction_Currency] ),
            DATESBETWEEN (
                'CORPDW FISCALYEARROLLINGPERIOD'[Day],
                MIN ( 'CORPDW FISCALYEARROLLINGPERIOD'[QTD_START_DATE] ),
                MAX ( 'CORPDW FISCALYEARROLLINGPERIOD'[QTD_END_DATE] )
            ),
            'CORPDW ACCOUNT'[AccType] = "Expense"
        ),
    SELECTEDVALUE ( 'Period Slicers'[Period Slicer] ) = "YTD",
        CALCULATE (
            SUM ( 'CORPDW TRIALBALANCE'[Amount_in_Transaction_Currency] ),
            DATESBETWEEN (
                'CORPDW FISCALYEARROLLINGPERIOD'[Day],
                MIN ( 'CORPDW FISCALYEARROLLINGPERIOD'[YTD_START_DATE] ),
                MAX ( 'CORPDW FISCALYEARROLLINGPERIOD'[YTD_END_DATE] )
            ),
            'CORPDW ACCOUNT'[AccType] = "Expense"
        ),
    BLANK ()
)

 

Can you please verify this again?

LOL, Yes!!!😀

@sufiyanakhtar can you share a sample of your date dimension table.
Also, which column is connected to the fact? 'Day' or 'Date' and you have both in case 'Day' is actually 'Date'

Helpful resources

Announcements
August 1 episode 9_no_dates 768x460.jpg

The Power BI Community Show

Watch the playback when Priya Sathy and Charles Webb discuss Datamarts! Kelly also shares Power BI Community updates.

Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

Top Solution Authors
Top Kudoed Authors