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
Anonymous
Not applicable

Replace Values Based Vased on Condition(s)

Hi Folks,

 

I have an issue that I can't seem to quite figure out. I have a table that has a project column with 3 separate numerical condition columns.

Original:

Crystal77_0-1652370478681.png

 

Expected:

Crystal77_1-1652370511863.png

 

I have the current form of the table as well as how I need the table to be formatted. Also, I can't remove any rows as otherwise that'll mess up the results. Any help would be appreciated.

 

Here's a link to the sample file:
https://drive.google.com/file/d/12VxDFGkf_vcO5LwoAmHla4jl-pDcU8xd/view?usp=sharing 

 

Thank you. 

 

1 ACCEPTED SOLUTION
lbendlin
Super User
Super User

That seems like a very inefficient format. Might want to consider unpivoting instead.

 

Anyway, here goes:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCijKz0pNLjFU0lEyBWJjIDZRitWhQMIILADBxpRJGCPZYUqZhAlUwoRyCYSvKZYwAwrklebkoFFUVmFOFxUWdFFhCU8naEFJuoShAVDECB7LlMoYwiMYLRWTJWNEMCQoURILAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, #"Condition 1" = _t, #"Condition 2" = _t, #"Condition 3" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"Condition 1", Int64.Type}, {"Condition 2", Int64.Type}, {"Condition 3", Int64.Type}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
    #"Replaced Value1" = Table.ReplaceValue(#"Added Index",each [Condition 1], each if Number.Mod([Index],3) = 0 then [Condition 1] else null ,Replacer.ReplaceValue,{"Condition 1"}),
    #"Replaced Value2" = Table.ReplaceValue(#"Replaced Value1",each [Condition 2], each if Number.Mod([Index],3) = 1 then [Condition 2] else null ,Replacer.ReplaceValue,{"Condition 2"}),
    #"Replaced Value3" = Table.ReplaceValue(#"Replaced Value2",each [Condition 3], each if Number.Mod([Index],3) = 2 then [Condition 3] else null ,Replacer.ReplaceValue,{"Condition 3"})
in
    #"Replaced Value3"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done".

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Thank you very much! This absolutely does the trick!

lbendlin
Super User
Super User

That seems like a very inefficient format. Might want to consider unpivoting instead.

 

Anyway, here goes:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCijKz0pNLjFU0lEyBWJjIDZRitWhQMIILADBxpRJGCPZYUqZhAlUwoRyCYSvKZYwAwrklebkoFFUVmFOFxUWdFFhCU8naEFJuoShAVDECB7LlMoYwiMYLRWTJWNEMCQoURILAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, #"Condition 1" = _t, #"Condition 2" = _t, #"Condition 3" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"Condition 1", Int64.Type}, {"Condition 2", Int64.Type}, {"Condition 3", Int64.Type}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
    #"Replaced Value1" = Table.ReplaceValue(#"Added Index",each [Condition 1], each if Number.Mod([Index],3) = 0 then [Condition 1] else null ,Replacer.ReplaceValue,{"Condition 1"}),
    #"Replaced Value2" = Table.ReplaceValue(#"Replaced Value1",each [Condition 2], each if Number.Mod([Index],3) = 1 then [Condition 2] else null ,Replacer.ReplaceValue,{"Condition 2"}),
    #"Replaced Value3" = Table.ReplaceValue(#"Replaced Value2",each [Condition 3], each if Number.Mod([Index],3) = 2 then [Condition 3] else null ,Replacer.ReplaceValue,{"Condition 3"})
in
    #"Replaced Value3"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done".

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.