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

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
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.

Top Solution Authors
Top Kudoed Authors