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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
NewbeePowerBI
Advocate I
Advocate I

How to use if else with two conditions using DAX

Hi,

I am new to Power BI and I have data like this in sales table. I want to create a new column Max_price_new where 0 should be replaced by the Max_price of earlier quarter.

 

e.g. This is the input table

articleyr&quaquarterMax_priceyear
100268582017Qtr 2Qtr 202017
100268582017Qtr 1Qtr 1195372017
100268582016Qtr 3Qtr 3195372016
100268582016Qtr 1Qtr 1195372016

 

output table should look like:

articleyr&quaquarterMax_priseyearMax_Price_new
100268582017Qtr 2Qtr 20201719537
100268582017Qtr 1Qtr 119537201719537
100268582016Qtr 3Qtr 319537201619537
100268582016Qtr 1Qtr 119537201619537

 

I have already written few formulas to find out:

1. Quarter number where the Max_Price is 0.

2. Find the earlier quarter number.

 

But I am struggling with how to find price of earlier quarter. i.e. in it should be something like

Max_Price_new = Max_Price where article= current article value and quarter = earlier quarter

 

How should I write this using DAX ?

2 ACCEPTED SOLUTIONS
Anonymous
Not applicable

Try using  ALLEXCEPT ( Sheet1[Article] ) instead of  ALL ( Sheet1 ).

View solution in original post

v-yulgu-msft
Employee
Employee

Hi @NewbeePowerBI,

 

Please refer to below calculated columns:

Quarter Num = RIGHT(Sheet1[quarter],1)

Last quarter =
IF (
    Sheet1[Max_prise] = 0,
    CALCULATE (
        MAX ( Sheet1[Quarter Num] ),
        FILTER (
            ALLEXCEPT ( Sheet1, Sheet1[article], Sheet1[year] ),
            Sheet1[Quarter Num] < EARLIER ( Sheet1[Quarter Num] )
        )
    ),
    Sheet1[Quarter Num]
)

Max_prise New =
LOOKUPVALUE (
    Sheet1[Max_prise],
    Sheet1[article], Sheet1[article],
    Sheet1[year], Sheet1[year],
    Sheet1[Quarter Num], Sheet1[Last quarter]
)

1.PNG

 

Best regards,
Yuliana Gu

Community Support Team _ Yuliana Gu
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

6 REPLIES 6
v-yulgu-msft
Employee
Employee

Hi @NewbeePowerBI,

 

Please refer to below calculated columns:

Quarter Num = RIGHT(Sheet1[quarter],1)

Last quarter =
IF (
    Sheet1[Max_prise] = 0,
    CALCULATE (
        MAX ( Sheet1[Quarter Num] ),
        FILTER (
            ALLEXCEPT ( Sheet1, Sheet1[article], Sheet1[year] ),
            Sheet1[Quarter Num] < EARLIER ( Sheet1[Quarter Num] )
        )
    ),
    Sheet1[Quarter Num]
)

Max_prise New =
LOOKUPVALUE (
    Sheet1[Max_prise],
    Sheet1[article], Sheet1[article],
    Sheet1[year], Sheet1[year],
    Sheet1[Quarter Num], Sheet1[Last quarter]
)

1.PNG

 

Best regards,
Yuliana Gu

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

Hi @v-yulgu-msft  Thanks for the solution. It worked! 

Anonymous
Not applicable

You say you found a way to get the previous quarter. In my example I have a sequence number for the quarters with 1 starting at the earliest and 4 being the latest.

 

I also add 1 to each price, so you can see that you are getting the correct values.

 

You could create a statement like this to get the previous quarter value:


PreviousQuarterMaxPrice = VAR QuarterSequence =
    MAX ( Sheet1[QuarterSequence] )
RETURN
    CALCULATE (
        MAX ( Sheet1[Max_price] ),
        Sheet1[QuarterSequence]
            = QuarterSequence - 1,
        ALL ( Sheet1 )
    )

 

articleYearQuarterquarteryearMax_priceQuarterSequencePreviousQuarterMaxPrice
100268582017Qtr 2Qtr 220170419537
100268582017Qtr 1Qtr 1201719537319538
100268582016Qtr 3Qtr 3201619538219539
100268582016Qtr 1Qtr 12016195391 

Hi, Thank you for your solution.

 

Removing  MAX from this MAX ( Sheet1[QuarterSequence] ) in your query gave me half the answer of my question.


But I also have many articles like this in the column "article". How to add article condition into query? I want each article's value to be displayed

 

e.g.

articleyr&quaquarterMax_priseyearMax_Price_new
100268582017Qtr 2Qtr 20201719537
100268582017Qtr 1Qtr 119537201719537
100268582016Qtr 3Qtr 319537201619537
100268582016Qtr 1Qtr 119537201619537
100267772016Qtr 2Qtr 20201610
100267772016Qtr 1Qtr 1102016 
Anonymous
Not applicable

Try using  ALLEXCEPT ( Sheet1[Article] ) instead of  ALL ( Sheet1 ).

Hi @Anonymous Using ALLExcept and adding condition for checking year, solved my problem. 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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