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

Replace non blanks with 1

All,

 

In Power Query, How do I replace all non blanks with 1

 

I have tried the Excel Find and Replace method of   *  to   1 

However this does not seem to work in Power BI

 

 

1 ACCEPTED SOLUTION
edhans
Super User
Super User

Hi @hdhillon , try this:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSi/KLy1QMFTSUQJhUwOlWB10QUMDrKLmpmiiRljVGuMUNUIRNYKai2KsEZoTgEwQAjETi1PSYNxYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Group = _t, Month = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Value", Int64.Type}, {"Month", Int64.Type}}),
    #"Replaced Value" = 
        Table.ReplaceValue(
            #"Changed Type",
            each [Group],
            each
                let
                    varReplacementValue = if Text.Length([Group]) > 0 then "1" else [Group]
                in
            varReplacementValue,
            Replacer.ReplaceText,
            {"Group"}
        )
in
    #"Replaced Value"

It turns this:

edhans_0-1604946596158.png

Into this:

edhans_1-1604946621629.png

It looks for any field that has a length greater than 0 and replaces that field with "1" if it does.

 

How to use M code provided in a blank query:
1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.

 

 



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

View solution in original post

4 REPLIES 4
sriram18in
New Member

Powerquery

= IFERROR(IF(LEN(related(column_name))>0,"Y","-"),"-")

watkinnc
Super User
Super User

I know you said replace, but you could easily add a column like this:

Table.AddColumn(PriorStep, "NewColumnName", each if [Column] <> null then 1 else [Column], type number)


I’m usually answering from my phone, which means the results are visualized only in my mind. You’ll need to use my answer to know that it works—but it will work!!
wdx223_Daniel
Super User
Super User

@hdhillon click the wdx223_Daniel_0-1605167304579.pngbutton beside the Edit Bar and input this formula

=Table.ReplaceValue(LastStepName,null,1,(x,y.z)=>if x=y or x="" then x else z,Table.ColumnNames(LastStepName))

LastStepName, is the last step name in your original M code

 

edhans
Super User
Super User

Hi @hdhillon , try this:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSi/KLy1QMFTSUQJhUwOlWB10QUMDrKLmpmiiRljVGuMUNUIRNYKai2KsEZoTgEwQAjETi1PSYNxYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Group = _t, Month = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Value", Int64.Type}, {"Month", Int64.Type}}),
    #"Replaced Value" = 
        Table.ReplaceValue(
            #"Changed Type",
            each [Group],
            each
                let
                    varReplacementValue = if Text.Length([Group]) > 0 then "1" else [Group]
                in
            varReplacementValue,
            Replacer.ReplaceText,
            {"Group"}
        )
in
    #"Replaced Value"

It turns this:

edhans_0-1604946596158.png

Into this:

edhans_1-1604946621629.png

It looks for any field that has a length greater than 0 and replaces that field with "1" if it does.

 

How to use M code provided in a blank query:
1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.

 

 



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

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