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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
prajwal1
Frequent Visitor

DAX measure correction

Hello,

I need your help to correct a dax expression.
I have a 'PIs info' table like below

PIPI start datePI end datePrevious PI code
PI 23-Q410-Sep-2315-Dec-23PI 23-Q4
PI 24-Q117-Dec-2311-Mar-24PI 23-Q4
PI 24-Q225-Mar-2414-Jun-24PI 24-Q1

 

I have another 'Tempo data' table like below

PI timing tagPI codeTime worked hrs
PI 23-Q4PI 23-Q44
PI 23-Q4PI 24-Q12
PI 24-Q1PI 23-Q43
PI 24-Q1PI 24-Q15
PI 24-Q1PI 24-Q24
PI 24-Q2PI 24-Q26
PI 24-Q2PI 24-Q13


When I select a PI in a slicer, then calculate the sum of Time worked hrs where the PI = PI timing tag and PI code in values of list of PI which are before the selected PI. FYI I don't have relationship between the tables (also cannot make it)
Example: PI 24-Q1 selected in slicer. The output I want 5+3=8. If PI 24-Q2 is selected, then it should 6+3=9.

I am using below DAX expression. But it is considering only one previous PI for the selected PI whereas it should consider all the previous PIs for the selected PI

Worked hrs for selected PI =
VAR Selected_PI = SELECTEDVALUE('PIs info'[PI])
VAR Previous_PI = SELECTEDVALUE('PIs info'[Previous PI code])
RETURN
    CALCULATE (
        SUM ( 'Tempo data'[timeworked] ),
        'Tempo data'[PI timing tag] = Selected_PI,
        'Tempo data'[pi_code] IN {Selected_PI, Previous_PI},
        'Tempo data'[weekstartdate] <= MAX('Tempo last updated date'[Logged till date])
    )
1 ACCEPTED SOLUTION

Hi @prajwal1 ,

 

Based on your additional instructions, please try the following steps:
My Sample:
PIs info:

vweiyan1msft_0-1710816156285.png

Tempo data:

vweiyan1msft_1-1710816173803.png

1.You can create a calculated table.

Table 2 = 
DATATABLE (
    "PI", STRING,
    "Order", INTEGER,
    {
        { "PI 23-Q3", 1 },
        { "PI 23-Q4", 2 },
        { "PI 24-Q1", 3 },
        { "PI 24-Q2", 4 }
    }
)

vweiyan1msft_2-1710816233222.png

2. Use the following code to create a measure.

Worked hrs for selected PI = 
VAR Selected_PI = SELECTEDVALUE('PIs info'[PI])
VAR PI_Order = MAXX(FILTER('Table 2','Table 2'[PI] = Selected_PI),'Table 2'[Order])
VAR Valid_PIs = 
    CALCULATETABLE(
        VALUES('Table 2'[PI]),
        'Table 2'[Order] <= PI_Order
    )
VAR Result = 
    CALCULATE(
        SUM('Tempo data'[Time worked hrs]),
        FILTER(
            'Tempo data',
            'Tempo data'[PI timing tag] = Selected_PI  &&
            'Tempo data'[PI code] IN Valid_PIs
        )
    )
RETURN
    IF(ISBLANK(Result), 0, Result)

When you select "PI 24-Q2" in the slicer, Result is as below.

vweiyan1msft_3-1710816266504.png


Best Regards,
Yulia Yan


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-weiyan1-msft
Community Support
Community Support

Hi @prajwal1 ,

 

@some_bih Thank you, for your quick response and the solution provided.
I am not sure how your semantic model looks like.
Based on the sample and description you provided, perhaps you can try code as below to create measure.
My Sample:
PIs info:

vweiyan1msft_0-1710751246751.png

Tempo data:

vweiyan1msft_1-1710751259167.png

Worked hrs for selected PI = 
VAR Selected_PI = SELECTEDVALUE('PIs info'[PI])
VAR Previous_PI = MAXX(FILTER('PIs info','PIs info'[PI] = Selected_PI),'PIs info'[Previous PI code])
VAR Result = 
    CALCULATE(
        SUM('Tempo data'[Time worked hrs]),
        FILTER(
            'Tempo data',
            'Tempo data'[PI timing tag] = Selected_PI &&
            ('Tempo data'[PI code] = Selected_PI || 'Tempo data'[PI code] = Previous_PI)
        )
    )
RETURN
    IF(ISBLANK(Result), 0, Result)

In the Visualizations pane, select the Card icon and put the measure in the card visualization.
When you select "PI 24-Q2" in the slicer, Result is as below.

vweiyan1msft_2-1710751305417.png

Is this the result you expect?


Best Regards,
Yulia Yan


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

Hi @v-weiyan1-msft . Thanks for your response. But in your logic it will always consider two PIs only. not more than that. What will happen if we have two previous PIs.?

For example the tables structure of Tempo data is like below

prajwal1_0-1710764569276.png

If I select PI 24-Q2 then it should display 6+3+1 = 10. Because it has two previous PIs.

Hi @prajwal1 ,

 

Based on your additional instructions, please try the following steps:
My Sample:
PIs info:

vweiyan1msft_0-1710816156285.png

Tempo data:

vweiyan1msft_1-1710816173803.png

1.You can create a calculated table.

Table 2 = 
DATATABLE (
    "PI", STRING,
    "Order", INTEGER,
    {
        { "PI 23-Q3", 1 },
        { "PI 23-Q4", 2 },
        { "PI 24-Q1", 3 },
        { "PI 24-Q2", 4 }
    }
)

vweiyan1msft_2-1710816233222.png

2. Use the following code to create a measure.

Worked hrs for selected PI = 
VAR Selected_PI = SELECTEDVALUE('PIs info'[PI])
VAR PI_Order = MAXX(FILTER('Table 2','Table 2'[PI] = Selected_PI),'Table 2'[Order])
VAR Valid_PIs = 
    CALCULATETABLE(
        VALUES('Table 2'[PI]),
        'Table 2'[Order] <= PI_Order
    )
VAR Result = 
    CALCULATE(
        SUM('Tempo data'[Time worked hrs]),
        FILTER(
            'Tempo data',
            'Tempo data'[PI timing tag] = Selected_PI  &&
            'Tempo data'[PI code] IN Valid_PIs
        )
    )
RETURN
    IF(ISBLANK(Result), 0, Result)

When you select "PI 24-Q2" in the slicer, Result is as below.

vweiyan1msft_3-1710816266504.png


Best Regards,
Yulia Yan


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

some_bih
Super User
Super User

Hi @prajwal1 

According to measure definition, part 'Tempo data'[pi_code]  is related to IN for two different columns, which is not possible.

Rework your measure

 

Worked hrs for selected PI =
VAR Selected_PI = SELECTEDVALUE('PIs info'[PI])
VAR Previous_PI = SELECTEDVALUE('PIs info'[Previous PI code])
RETURN
CALCULATE (
SUM ( 'Tempo data'[timeworked] ),
'Tempo data'[PI timing tag] = Selected_PI,
'Tempo data'[pi_code] IN {Selected_PI, Previous_PI},
'Tempo data'[weekstartdate] <= MAX('Tempo last updated date'[Logged till date])
)





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

Proud to be a Super User!






Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.