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
KatiS
Frequent Visitor

Creating a new column based on if conditions from the combined parameters of other columns

Hi everyone,

 

I have a bit of a complicated situation where I would like to create a conditional column called Ext Net Cost (EXN) - the last column below, based on the following sample data table and if statements below the table. 

 

The Sample data is as follows:

      
Project numberTime Period (TP)Net cost (NC)Actual Ext. Gross Cost (AEGC)Fcst Ext Net Cost (FENC)Ext Net Cost (EXN)
1YTD01000
1HY01000
1FY0000
2YTD01000
2HY010100
2FY00100
3YTD6030030
3HY30101010
3FY0000
4YTD20070070
4HY50505050
4FY2500200200
5YTD3001500150
5HY100100100100
5FY5000120150
     

 

The if statements are:

if NC HY = 0 and NC FY =0 then EXN FY/HY/YTD = 0 (read as if value of NC for HY period is zero and value of NC for for FY period is zero then return 0 for all periods in the EXN column)

else

if NC HY > 0 and NC FY =0 then EXN FY = 0 and EXN HY = AEGC HY (read as if value of NC for HY period is > zero and value of NC for for FY period is zero then return 0 in the FY row of the EXN column and return the value from the AEGC column for HY period in the HY row if the EXN column etc.)

else

if NC HY > 0 and NC FY >0 and AEGC YTD <= FENC FY then EXN FY = FENC FY, EXN HY = AEGC HY

else

if NC HY > 0 and NC FY >0 and AEGC YTD > FENC FY then EXN FY = AEGC YTD, EXN HY = AEGC HY

else

EXN YTD = AEGC YTD

 

Please note this conditional evaluation should be done for each project in the list and the number of projects and project numbers are variables that can change so specific project numbers should not be included in the code. 

 

I have been struggling with this so your help will be greatly appreciated!

Thank you

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @KatiS ,

Try to pivot from NC, AEGC, and FENC by TP column, then the next conditional sentence will be easier.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("dc/NDYAgDAXgXThzoK3VBQxxAC+EsP8a8mOLKB5oyHvhA2I0YKwJ556nywtc3STbiiPMcy95j/HPwY8DvRigOyeV1pJSp0goels0f9SiFLqSbmNVLXaPIU21kEUrp0vF6lH1gDvIAkKrdErp2236W8xVugA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Project number" = _t, #"Time Period (TP)" = _t, #"Net cost (NC)" = _t, #"Actual Ext. Gross Cost (AEGC)" = _t, #"Fcst Ext Net Cost (FENC)" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Project number", Int64.Type}, {"Time Period (TP)", type text}, {"Net cost (NC)", Int64.Type}, {"Actual Ext. Gross Cost (AEGC)", Int64.Type}, {"Fcst Ext Net Cost (FENC)", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Project number", "Time Period (TP)"}, {{"Data", each _, type table [Project number=nullable number, #"Time Period (TP)"=nullable text, #"Net cost (NC)"=nullable number, #"Actual Ext. Gross Cost (AEGC)"=nullable number, #"Fcst Ext Net Cost (FENC)"=nullable number]}}),
    #"Pivoted Column" = Table.Pivot(#"Grouped Rows", List.Distinct(#"Grouped Rows"[#"Time Period (TP)"]), "Time Period (TP)", "Data"),
    #"Expanded YTD" = Table.ExpandTableColumn(#"Pivoted Column", "YTD", {"Net cost (NC)", "Actual Ext. Gross Cost (AEGC)", "Fcst Ext Net Cost (FENC)"}, {"Net cost (NC)(YTD)", "Actual Ext. Gross Cost (AEGC)(YTD)", "Fcst Ext Net Cost (FENC)(YTD)"}),
    #"Expanded HY" = Table.ExpandTableColumn(#"Expanded YTD", "HY", {"Net cost (NC)", "Actual Ext. Gross Cost (AEGC)", "Fcst Ext Net Cost (FENC)"}, {"Net cost (NC)(HY)", "Actual Ext. Gross Cost (AEGC)(HY)", "Fcst Ext Net Cost (FENC)(HY)"}),
    #"Expanded FY" = Table.ExpandTableColumn(#"Expanded HY", "FY", {"Net cost (NC)", "Actual Ext. Gross Cost (AEGC)", "Fcst Ext Net Cost (FENC)"}, {"Net cost (NC)(FY)", "Actual Ext. Gross Cost (AEGC)(FY)", "Fcst Ext Net Cost (FENC)(FY)"})
in
    #"Expanded FY"

vcgaomsft_0-1719797928099.png

Next, create three custom columns EXN(YTD), EXN(HY), EXN(FY). Please feel free to contact me if you need further guidance.

Best Regards,
Gao

Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

How to get your questions answered quickly --  How to provide sample data in the Power BI Forum -- China Power BI User Group

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Hi @KatiS ,

Try to pivot from NC, AEGC, and FENC by TP column, then the next conditional sentence will be easier.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("dc/NDYAgDAXgXThzoK3VBQxxAC+EsP8a8mOLKB5oyHvhA2I0YKwJ556nywtc3STbiiPMcy95j/HPwY8DvRigOyeV1pJSp0goels0f9SiFLqSbmNVLXaPIU21kEUrp0vF6lH1gDvIAkKrdErp2236W8xVugA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Project number" = _t, #"Time Period (TP)" = _t, #"Net cost (NC)" = _t, #"Actual Ext. Gross Cost (AEGC)" = _t, #"Fcst Ext Net Cost (FENC)" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Project number", Int64.Type}, {"Time Period (TP)", type text}, {"Net cost (NC)", Int64.Type}, {"Actual Ext. Gross Cost (AEGC)", Int64.Type}, {"Fcst Ext Net Cost (FENC)", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Project number", "Time Period (TP)"}, {{"Data", each _, type table [Project number=nullable number, #"Time Period (TP)"=nullable text, #"Net cost (NC)"=nullable number, #"Actual Ext. Gross Cost (AEGC)"=nullable number, #"Fcst Ext Net Cost (FENC)"=nullable number]}}),
    #"Pivoted Column" = Table.Pivot(#"Grouped Rows", List.Distinct(#"Grouped Rows"[#"Time Period (TP)"]), "Time Period (TP)", "Data"),
    #"Expanded YTD" = Table.ExpandTableColumn(#"Pivoted Column", "YTD", {"Net cost (NC)", "Actual Ext. Gross Cost (AEGC)", "Fcst Ext Net Cost (FENC)"}, {"Net cost (NC)(YTD)", "Actual Ext. Gross Cost (AEGC)(YTD)", "Fcst Ext Net Cost (FENC)(YTD)"}),
    #"Expanded HY" = Table.ExpandTableColumn(#"Expanded YTD", "HY", {"Net cost (NC)", "Actual Ext. Gross Cost (AEGC)", "Fcst Ext Net Cost (FENC)"}, {"Net cost (NC)(HY)", "Actual Ext. Gross Cost (AEGC)(HY)", "Fcst Ext Net Cost (FENC)(HY)"}),
    #"Expanded FY" = Table.ExpandTableColumn(#"Expanded HY", "FY", {"Net cost (NC)", "Actual Ext. Gross Cost (AEGC)", "Fcst Ext Net Cost (FENC)"}, {"Net cost (NC)(FY)", "Actual Ext. Gross Cost (AEGC)(FY)", "Fcst Ext Net Cost (FENC)(FY)"})
in
    #"Expanded FY"

vcgaomsft_0-1719797928099.png

Next, create three custom columns EXN(YTD), EXN(HY), EXN(FY). Please feel free to contact me if you need further guidance.

Best Regards,
Gao

Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

How to get your questions answered quickly --  How to provide sample data in the Power BI Forum -- China Power BI User Group

Hi Gao,

This way of looking at it simplifies it significantly and it is easy to apply the if conditions and can unpivot after. It worked. Thank you!  🙂 

Kat 

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.