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
RodCamargoJr
Helper I
Helper I

Substitute negative values on columns based on their position

Hi guys,

I'm trying to substitute the negatives values of my table to 0 here on Power Query,

 

I have 24 fixed columns on my table that I need to make such adjustments,

But the problem is that these columns change their name every week,

Now I'm using a basic IF ELSE to adjust the values, but every time the columns change their name, I have to adjust the code,

This is one example of my current code, for one column, 

Example:
#"New Column" = Table.AddColumn(#"02/2021 (ajustado)", "New Column", each if [#"03/2021"] < 0 then 0 else [#"03/2021"]),

The column I'm adjusting is named 03/2021, but its position is {13},

How can I adjust the code to get the column by its position rather than its name?

2 ACCEPTED SOLUTIONS
ams1
Super User
Super User

Hi,

 

Without seeing some sample data, I can't be sure this is the best solution, but:

 

GIVEN the following input:

ams1_0-1676532817616.png

 

WHEN:

 

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content], // Your table should be here
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", Int64.Type}, {"Column2", Int64.Type}, {"Column3", Int64.Type}, {"Column4", Int64.Type}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1, Int64.Type),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Added Index", {"Index"}, "Attribute", "Value"),
    #"Added Custom" = Table.AddColumn(#"Unpivoted Other Columns", "NewValue", each if [Value] < 0 then 0 else [Value]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Value"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Attribute]), "Attribute", "NewValue"),
    #"Removed Columns1" = Table.RemoveColumns(#"Pivoted Column",{"Index"})
in
    #"Removed Columns1"

 

 

THEN you will get the values replaced:

ams1_1-1676532911044.png

Using UNPIVOT, you move the columns to rows so that column names don't matter anymore.

This could also be addapted so that you can remove certain columns from replacement etc.

 

It might be like shooting an ant with a cannon, but there are a lot of unknowns and you should definitely know/read about UNPIVOT 😊 #unpivotAnything.

 

Please mark this as answer if it helped.

View solution in original post

wdx223_Daniel
Super User
Super User

#"New Column" = Table.AddColumn(#"02/2021 (ajustado)", "New Column", each let a=Record.Field(_,Record.FieldNames(_){13}) in  if a < 0 then 0 else a),

View solution in original post

3 REPLIES 3
wdx223_Daniel
Super User
Super User

#"New Column" = Table.AddColumn(#"02/2021 (ajustado)", "New Column", each let a=Record.Field(_,Record.FieldNames(_){13}) in  if a < 0 then 0 else a),

ams1
Super User
Super User

Hi,

 

Without seeing some sample data, I can't be sure this is the best solution, but:

 

GIVEN the following input:

ams1_0-1676532817616.png

 

WHEN:

 

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content], // Your table should be here
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", Int64.Type}, {"Column2", Int64.Type}, {"Column3", Int64.Type}, {"Column4", Int64.Type}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1, Int64.Type),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Added Index", {"Index"}, "Attribute", "Value"),
    #"Added Custom" = Table.AddColumn(#"Unpivoted Other Columns", "NewValue", each if [Value] < 0 then 0 else [Value]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Value"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Attribute]), "Attribute", "NewValue"),
    #"Removed Columns1" = Table.RemoveColumns(#"Pivoted Column",{"Index"})
in
    #"Removed Columns1"

 

 

THEN you will get the values replaced:

ams1_1-1676532911044.png

Using UNPIVOT, you move the columns to rows so that column names don't matter anymore.

This could also be addapted so that you can remove certain columns from replacement etc.

 

It might be like shooting an ant with a cannon, but there are a lot of unknowns and you should definitely know/read about UNPIVOT 😊 #unpivotAnything.

 

Please mark this as answer if it helped.

Thanks @ams1 , the feature of Pivot/Unpivot is really useful, it can work on multiples cenarios, and even, help you adjust multiples columns at once. Thanks again.

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