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

a more efficient way to count between X numbers (columns)?

Hi.

I have a column (around 5k rows) with text type values  similar to this 145-157-169-181-200, this numbers represents frames and I need the diference between frames, something like 12-12-12-19.

I have achieved the desired result doing split column by delimiter "-" then a custom column for each frame calculation (col2-col1, col3-col2, col4-col3, etc.) and a merge between all custom columns, final result (12-12-12-19). The problem is that I have values with more than 30 different frames and things get complicated with 30 columns or more. So is there a more efficient way to achieve the result?

Regards, Liu

2 ACCEPTED SOLUTIONS

My suggestion would be to create a custom function Differences:

 

(String as nullable text) as nullable text =>
let
    List = Text.Split(String,"-"),
    Numbers = List.Transform(List, Number.From),
    NumbersWithPrevious = List.Zip({List.Skip(Numbers),List.FirstN(Numbers,List.Count(Numbers)-1)}),
    Subtracted = List.Transform(NumbersWithPrevious, each _{0}-_{1}),
    Texts = List.Transform(Subtracted, Text.From),
    Combined = Text.Combine(Texts,"-")
in
    Combined

 

Now you can add a column to your table with option "Invoke Custom Function".
Resulting query (I manually added , type text in the second line):

 

let
    Source = #table(type table[Frames = text],{{"145-157-169-181-200"},{"145-200"}}),
    #"Invoked Custom Function" = Table.AddColumn(Source, "Differences", each Differences([Frames]), type text)
in
    #"Invoked Custom Function"

 

Specializing in Power Query Formula Language (M)

View solution in original post

Probably you have an older version of Power Query without List.Zip.

 

An alternative is to use Table.FromColumns and - in the next step - subtract the columns using Table.TransformRows.

 

Table.TransformRows is probably not well known with a broader audience.
Basicallly it does the same as adding a column to a table and next drill down into that column (resulting in a list with only the values from the added column, so all other columns deleted).

 

Revised code for function Differences:

 

 

(String as nullable text) as nullable text =>
let
    List = Text.Split(String,"-"),
    Numbers = List.Transform(List, Number.From),
    NumbersWithPrevious = Table.FromColumns({List.Skip(Numbers),List.FirstN(Numbers,List.Count(Numbers)-1)}),
    Subtracted = Table.TransformRows(NumbersWithPrevious, each [Column1]-[Column2]),
    Texts = List.Transform(Subtracted, Text.From),
    Combined = Text.Combine(Texts,"-")
in
    Combined

 

Specializing in Power Query Formula Language (M)

View solution in original post

5 REPLIES 5

The general approach I would suggest is to

  • Add an Index column
  • split into columns as before
  • unpivot the new columns

this should give you a table with one row per frame.  Then with each index number, you should have rows that represent the frames. Then use the technique is show in this video to find the previous record and create the difference column. https://m.youtube.com/watch?v=xN2IRXQ2CvI



* Matt is an 8 times Microsoft MVP (Power BI) and author of the Power BI Book Supercharge Power BI.

My suggestion would be to create a custom function Differences:

 

(String as nullable text) as nullable text =>
let
    List = Text.Split(String,"-"),
    Numbers = List.Transform(List, Number.From),
    NumbersWithPrevious = List.Zip({List.Skip(Numbers),List.FirstN(Numbers,List.Count(Numbers)-1)}),
    Subtracted = List.Transform(NumbersWithPrevious, each _{0}-_{1}),
    Texts = List.Transform(Subtracted, Text.From),
    Combined = Text.Combine(Texts,"-")
in
    Combined

 

Now you can add a column to your table with option "Invoke Custom Function".
Resulting query (I manually added , type text in the second line):

 

let
    Source = #table(type table[Frames = text],{{"145-157-169-181-200"},{"145-200"}}),
    #"Invoked Custom Function" = Table.AddColumn(Source, "Differences", each Differences([Frames]), type text)
in
    #"Invoked Custom Function"

 

Specializing in Power Query Formula Language (M)

@MarcelBeug I tried your suggestion and got this error :  Expression error: The name 'List.Zip' wasn't recognized.  Make sure it's spelled correctly. I know it's well spelled, so i'm kinda lost here Smiley Frustrated

Probably you have an older version of Power Query without List.Zip.

 

An alternative is to use Table.FromColumns and - in the next step - subtract the columns using Table.TransformRows.

 

Table.TransformRows is probably not well known with a broader audience.
Basicallly it does the same as adding a column to a table and next drill down into that column (resulting in a list with only the values from the added column, so all other columns deleted).

 

Revised code for function Differences:

 

 

(String as nullable text) as nullable text =>
let
    List = Text.Split(String,"-"),
    Numbers = List.Transform(List, Number.From),
    NumbersWithPrevious = Table.FromColumns({List.Skip(Numbers),List.FirstN(Numbers,List.Count(Numbers)-1)}),
    Subtracted = Table.TransformRows(NumbersWithPrevious, each [Column1]-[Column2]),
    Texts = List.Transform(Subtracted, Text.From),
    Combined = Text.Combine(Texts,"-")
in
    Combined

 

Specializing in Power Query Formula Language (M)

magnificent! Thanks Marcel Smiley Happy

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.