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
paololito
Helper I
Helper I

New calculated column too complex for me .

Hello ,

I hope that you can help me on this issue because I'm little disapointed about it .

My level in Dax is not sufficient to archive this .

 

I have a table 'NewPBI" I would like create a calculated column "NewAmount"  with this king of approach 

let me explain with some examples 

Situation 1 : Scenario A = Scenario B  and only one  row for the combinaison Scenario A and G/L# so newAmount should be Amount

paololito_4-1660492518192.png

 

situation 2  : Scenario A <> Scenario B  and only one  row for the combinaison Scenario A and G/L# so newAmount should be the amount of the combination Scenario A and G/L#

paololito_0-1660495114918.png

 

Situation 3  : multiple result for the combination of Scenario A and G/L # 

 Scenario A = Scenario B then NewAmount = Amount

Scenario A <> Scenario B the NewAmount = 0

paololito_5-1660492879660.png

 

the dax formula seems very complex so i hope that you can help me on this .

 

Thank you very much

 

Paololito

 

 

 

1 ACCEPTED SOLUTION

Try this:

New Amount = 
VAR MaxScenarioBForCurrentGroup =
    CALCULATE (
        MAX ( 'Table'[Scenario B] ),
        ALLEXCEPT ( 'Table', 'Table'[Scenario A], 'Table'[G/L] )
    )
VAR NewAmount =
    // choose  expressions - both give same result
    //IF ( 'Table'[Scenario B] = MaxScenarioBForCurrentGroup, 'Table'[Amount], 0 )
    CALCULATE( SUM('Table'[Amount]), 'Table'[Scenario B] = MaxScenarioBForCurrentGroup )
RETURN
    NewAmount

View solution in original post

6 REPLIES 6
latimeria
Solution Specialist
Solution Specialist

Hi @paololito ,

My understanding of your problem:
scenario A & G/L : unique => amount
                              not unique, scenario A = scenario B => amount else 0

Data, copy in powerquery

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WckwuKU3MUTAyMDRS0kHjGRobGhgYABm6hiAGkBmrQ0iHEVSHKRb1JijqjVFtwGaBGYoGUxQN2CwwI9kLqDrMUHUQtsIUxc+GEPWxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Scenario A" = _t, #"Scenario B" = _t, #"G/L" = _t, Amount = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Amount", type number}})
in
    #"Changed Type"

 

Dax formula for column:

New Amount = 
VAR CurrentScenarioGL = 'Table'[Scenario A] & 'Table'[G/L]
VAR CountScenarioGL =
    CALCULATE (
        COUNTROWS ( 'Table' ),
        REMOVEFILTERS ( 'Table' ),
        'Table'[Scenario A] & 'Table'[G/L] = CurrentScenarioGL
    )
VAR NewAmount =
    SWITCH (
        TRUE (),
        CountScenarioGL = 1, 'Table'[Amount],
        'Table'[Scenario A] = 'Table'[Scenario B], 'Table'[Amount],
        0
    )
RETURN
    NewAmount

Pay attention, 'switch' stop evaluation when first condition is met.

latimeria_0-1660498981969.png


Hope this help.

@latimeria  Thank you very much . indeed this is very usefull.

I didn't understand yet your formula but , I will take time to understand it for sure .

 

However I forgot to put another scenario , sorry for that .

in the situation below Scenario A = Actual 2017 and Scenario B = Actual 2016  NewValue  should contain the last known value when scenario A =  Scenario B 

the purpose of this script is to have a value for each scenario A.

In this situation the last value is Scenario A = Actual 2016 and Scenario B = Actual 2016

paololito_0-1660504548730.png

I hope you can help me again , I cross my finger 😉

 

paololito

Try this:

New Amount = 
VAR MaxScenarioBForCurrentGroup =
    CALCULATE (
        MAX ( 'Table'[Scenario B] ),
        ALLEXCEPT ( 'Table', 'Table'[Scenario A], 'Table'[G/L] )
    )
VAR NewAmount =
    // choose  expressions - both give same result
    //IF ( 'Table'[Scenario B] = MaxScenarioBForCurrentGroup, 'Table'[Amount], 0 )
    CALCULATE( SUM('Table'[Amount]), 'Table'[Scenario B] = MaxScenarioBForCurrentGroup )
RETURN
    NewAmount

@latimeria , I try to understand your code .

Can you give me some explanation about the usage of ALLEXCEPT .

Normally ALLEXCEPT mean ALL (table) except filter context but I have no filter context on my table .  does it mean that when you calculate Row by Row the value of a column became a filter context ?

 

Hi @paololito ,

Calculate works only with filters context, not with row context, so you"re right the row context is tranformed into a filter context. 

you can find the explantion here: Understanding context transition in DAX - SQLBI


Steps for creating new column with calculate: (not all steps explained, there more steps) for each row:

  • save the current filter context
  • transition the row context into filter context. basically, filter all fields with the current field's value
  • apply the new context, allexcept = remove (not filter added!) any active filters on all fields except  on columns Table'[Scenario A] & 'Table'[G/L] .
  • get the max value for 'Table'[Scenario B] in this new context, with current '[Scenario A]  & [G/L]
  • restore the current context

The most important thing to remind is that the evaluation happens after filering.

screen shot after the calculate:

latimeria_0-1660893150761.png

usefull links:
ALLEXCEPT – DAX Guide

Introducing CALCULATE in DAX - SQLBI

@latimeria It works fine  . thank you very much for your support .

your dax code seems be very simple 😉 

I choose the IF expression because the other one don't give the result expected .

 

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.