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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Anonymous
Not applicable

Conditional replace values

Hi, how can i replaced 0 to null on all columns but only for Category C and D in power query/M code?

 

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjVQ0lGyABFmIMIIRDgqxepEK4FYhiDCBESAsBNY3BjGBWFLEOEMV2+Eot5FKTYWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t, Category = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", Int64.Type}, {"Column2", Int64.Type}, {"Column3", Int64.Type}, {"Column4", Int64.Type}, {"Category", type text}})
in
#"Changed Type"


From:

Column1Column2Column3Column4Category
50806020A
010400B
300090C
020400D

 

To:

Column1Column2Column3Column4Category
50806020A
010400B
30nullnull90C
null2040nullD
1 ACCEPTED SOLUTION
KiwiPete
Frequent Visitor

Try this if you do not wish to use the filetered table route. 

The [If Then] makes the Replace hugely powerfull but might be slower as it is compares at the record level.  Be very interested to see which is faster across your very large data set.

 

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjVQ0lGyABFmIMIIRDgqxepEK4FYhiDCBESAsBNY3BjGBWFLEOEMV2+Eot5FKTYWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t, Category = _t]),
Columns = Table.ColumnNames(Source),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", Int64.Type}, {"Column2", Int64.Type}, {"Column3", Int64.Type}, {"Column4", Int64.Type}, {"Category", type text}}),
#"Replaced Value" = Table.ReplaceValue(#"Changed Type",0,each if [Category] = "C" or [Category] = "D" then "null" else 0 ,Replacer.ReplaceValue,{"Column1","Column2","Column3","Column4"})
in
#"Replaced Value"

View solution in original post

4 REPLIES 4
KiwiPete
Frequent Visitor

Try this if you do not wish to use the filetered table route. 

The [If Then] makes the Replace hugely powerfull but might be slower as it is compares at the record level.  Be very interested to see which is faster across your very large data set.

 

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjVQ0lGyABFmIMIIRDgqxepEK4FYhiDCBESAsBNY3BjGBWFLEOEMV2+Eot5FKTYWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t, Category = _t]),
Columns = Table.ColumnNames(Source),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", Int64.Type}, {"Column2", Int64.Type}, {"Column3", Int64.Type}, {"Column4", Int64.Type}, {"Category", type text}}),
#"Replaced Value" = Table.ReplaceValue(#"Changed Type",0,each if [Category] = "C" or [Category] = "D" then "null" else 0 ,Replacer.ReplaceValue,{"Column1","Column2","Column3","Column4"})
in
#"Replaced Value"

KiwiPete
Frequent Visitor

Split the table into two separate tables, one with CD, one without CD.  Replace all 0's across the with CD then rejoin the tables.

 

let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
FilterNonD = Table.SelectRows(Source, each not ([Category] = "D" or [Category] = "C")),
FilterD = Table.SelectRows(Source, each [Category] = "D" or [Category] = "C"),
ReplaceZeros = Table.ReplaceValue(FilterD,0,null,Replacer.ReplaceValue,{"Column1", "Column2", "Column3", "Column4"}),
CombineTables = Table.Combine({FilterNonD, ReplaceZeros})
in
CombineTables

Anonymous
Not applicable

I actually have huge data of million rows, is there a way without the need to split table?

@Anonymous , you might want to try,

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("VY65FcAwCEN3oXYRwFfKHFv4ef81EvnAScF/GEmGUihs5CgDERDgoOoKoWPAA6izzXU+UTtwmV9+/tvmPgPxhSYLqB8+yaNZH7HOFEcTWrvOCraiuZU/d/W5xnkTQ2RZiblSmiV1oT4=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t, Category = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", Int64.Type}, {"Column2", Int64.Type}, {"Column3", Int64.Type}, {"Column4", Int64.Type}, {"Category", type text}}),

    Columns = Table.ColumnNames(Source),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Category"}, {{"ar", each _, type table [Column1=nullable number, Column2=nullable number, Column3=nullable number, Column4=nullable number, Category=nullable text]}}),

    #"Added Custom" = Table.AddColumn(
        #"Grouped Rows",
        "Custom",
        each if List.Contains({"C","D"},[Category]) then
            Table.ReplaceValue([ar],0,null,Replacer.ReplaceValue,Columns)
            else [ar]
    ),

    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Custom"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Other Columns", "Custom", Columns, Columns)
in
    #"Expanded Custom"

Screenshot 2021-02-18 000128.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.