cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Imad-R
Regular Visitor

Replace value for several column depend condition power query

Hello,

 

The following will be an example of my problem. 

Here are the data : 

ImadR_0-1669665470367.png

 

I want to filter these data by type of stock so I unpivot the column related to the stock number, and we obtain this table :

ImadR_1-1669665946687.png

 

Only values different to 0 are interesting in the "Valeur" column consequently that's the result :

ImadR_2-1669666180231.png

My problem it's the following, I want to replace some value by 0 depending condition.

For exemple let's focus on the second article:

ImadR_3-1669666832918.png

 

I want to replace cupboard 1 stock value by 0 when modality in "Attribut" column equal "Freezer stock" for the first line, and replace Freezer stock value by 0 when modality = "cupboard 1 stock" for the second line. I want there to be only the value for the stock concerned and not another value for another type of stock that is different to the type of stock entered on the line. And that's for all line and all column concern value of stock. 

 

I have try to write with a M request but it doesn't work. 

 

Someone can help me please !!!

1 ACCEPTED SOLUTION
jbwtp
Memorable Member
Memorable Member

Hi @Imad-R,

 

See if the code below works as you want. You can then modified to suite your case:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wck7MTlXSUTIEYrei1NQqpVgdFEHXnOJUpdhYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, Value = _t, Attribute = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"Value", Int64.Type}, {"Attribute", type text}}),
    #"Replaced Value" = Table.ReplaceValue(#"Changed Type",each [Attribute], null, (x, y, z)=> if y= "Freez" then 0 else x,{"Value"})
in
    #"Replaced Value"

 

Cheers,

John

View solution in original post

2 REPLIES 2
jbwtp
Memorable Member
Memorable Member

Hi @Imad-R,

 

See if the code below works as you want. You can then modified to suite your case:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wck7MTlXSUTIEYrei1NQqpVgdFEHXnOJUpdhYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, Value = _t, Attribute = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"Value", Int64.Type}, {"Attribute", type text}}),
    #"Replaced Value" = Table.ReplaceValue(#"Changed Type",each [Attribute], null, (x, y, z)=> if y= "Freez" then 0 else x,{"Value"})
in
    #"Replaced Value"

 

Cheers,

John

Hello,

 

Thanks, it's working. It is not very convenient because I must make 3 different M requests for each modality of Attribut column but it suits me.

 

Thanks for your reactivity !

 

-- 1
= Table.ReplaceValue(#"Lignes filtrées",each [Attribut], null, (x, y, z)=> if y = "Freezer stock" then 0 else x,{"cupboard 1 stock value", "cupboard 2 stock value"})

-- 2
= Table.ReplaceValue(#"Personnalisé1",each [Attribut], null, (x, y, z)=> if y = "cupboard 1 stock" then 0 else x,{"Freezer stock value", "cupboard 2 stock value"})
-- 3
= Table.ReplaceValue(#"Personnalisé2",each [Attribut], null, (x, y, z)=> if y = "cupboard 2 stock" then 0 else x,{"Freezer stock value", "cupboard 1 stock value"})

 

Helpful resources

Announcements
Carousel_PBI_Wave1

2023 Release Wave 1 Plans

Power BI release plans for 2023 release wave 1 describes all new features releasing from April 2023 through September 2023.

Power BI Summit Carousel 2

Global Power BI Training

Make sure you register today for the Power BI Summit 2023. Don't miss all of the great sessions and speakers!

BizApps LATAM 2023

Business Application LATAM Summit 2023

Join the biggest FREE Business Applications Event in LATAM this February.

Power Platform Bootcamp

Global Power Platform Bootcamp

In this bootcamp we will deep-dive into Microsoft’s Power Platform stack with hands-on sessions and labs, delivered to you by experts and community leaders.

Top Solution Authors
Top Kudoed Authors