0
votes

In power query I would like to unpivot my table based on a condition: unpivot all columns except those that start with "IP_address" or "Please state your".

Is this possible?

1

1 Answers

0
votes

Here's one way:

If you start with a table like this:

enter image description here

You can click the enter image description here to the left of the formula bar and replace what appears in the formula bar with this:

#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, List.RemoveNulls(List.Generate(
()=>[A=Table.ColumnNames(Source), Index=0],
each [Index] < List.Count([A]),
each [A=[A],Index =[Index]+1],
each if Text.Start([A]{[Index]},10) = "IP_Address" then [A]{[Index]} 
    else if Text.Start([A]{[Index]},18) = "Please state your" then [A]{[Index]} 
    else null 
)), "Attribute", "Value")

To get this:

enter image description here

Here my complete query code:

let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, List.RemoveNulls(List.Generate(
()=>[A=Table.ColumnNames(Source), Index=0],
each [Index] < List.Count([A]),
each [A=[A],Index =[Index]+1],
each if Text.Start([A]{[Index]},10) = "IP_Address" then [A]{[Index]} 
    else if Text.Start([A]{[Index]},18) = "Please state your" then [A]{[Index]} 
    else null 
)), "Attribute", "Value")
in
#"Unpivoted Other Columns"

I just noticed I used "IP_Address" (with a capital A) instead of "IP_address" (with a lower case A), so you'll need to adjust for that.