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

Variable Column list in List.Product & Lict.Nonnullcount

Hi, I am trying to transform : 
#"Inserted Multiplication" = Table.AddColumn(#"Renamed Columns", "Multiplication", each List.Product({[Column1], [Column2], [Column3], [Column4], [Column5], [ColumnBASE]}), Currency.Type),
and
#"Inserted Count" = Table.AddColumn(#"Added Index", "Count", each List.NonNullCount({[Column1], [Column2], [Column3], [Column4], [Column5]}), Int64.Type),

so that it can accomodate a variable list of columns (it needs to use all the columns with a name starting with "Column")

I am replacing  {[Column1], [Column2], [Column3], [Column4], [Column5]} by 
List.Select(Table.ColumnNames(#"Added Index"), each Text.Contains(_,"Column"))
but the count returns the same value for each row (which is incorrect) and the list.Product returns an error.
Seems to be a type error ( list is not considered as a list of columns?) 

Can this be solved ?  thank you 

1 ACCEPTED SOLUTION
mahoneypat
Employee
Employee

It might be easier to unpivot your data to do this; however, here is an example of how to do this with some example data.  To see how it works, just create a blank query, go to Advanced Editor, and replace the text there with the M code below.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTICYmMgNlGK1YlG4ukomSrFxgIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Text = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", Int64.Type}, {"Column2", Int64.Type}, {"Column3", Int64.Type}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each #"Added Index"{[Index]}),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom.1", each List.Sum(Record.ToList(Record.SelectFields([Custom], List.Select(Table.ColumnNames(#"Added Index"), each Text.Contains(_, "Column"))))))
in
    #"Added Custom1"

 

Regards,

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

2 REPLIES 2
mahoneypat
Employee
Employee

It might be easier to unpivot your data to do this; however, here is an example of how to do this with some example data.  To see how it works, just create a blank query, go to Advanced Editor, and replace the text there with the M code below.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTICYmMgNlGK1YlG4ukomSrFxgIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Text = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", Int64.Type}, {"Column2", Int64.Type}, {"Column3", Int64.Type}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each #"Added Index"{[Index]}),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom.1", each List.Sum(Record.ToList(Record.SelectFields([Custom], List.Select(Table.ColumnNames(#"Added Index"), each Text.Contains(_, "Column"))))))
in
    #"Added Custom1"

 

Regards,

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


HI Pat,

It works ! Legend !

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.