Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
ELOO
Frequent Visitor

calculate days if ID is the same and record order (separate column) is consecutive

  Hi can you help with providing detailed instructions for making sum of values in column C on the condition that number is column A is the same and record order in column B is consecutive. 

 

the solution for below example is

for 123456 the solution is 7

for 234567 the solution is 11

for 345678 the solution is 11, and 52

for 456789 the solution is 23 and 59

 

NUMBERRECORD_ORDERdatediff
45678930
45678963
456789756
12345636
12345641
23456784
23456797
34567837
34567844
34567879
345678843
456789223
3 ACCEPTED SOLUTIONS

The query will work, just edit ChangedType step and set text format for [Number] column.


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

View solution in original post

it was a great question.

use the next code to reach this result

 

Omid_Motamedise_0-1725577458348.png

 

let
    Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    #"Grouped Rows" = Table.Group(Source, {"NUMBER"}, {{"sum", each Table.Group(Table.AddColumn(Table.AddIndexColumn(Table.Sort(_,"RECORD_ORDER"), "Index", 0, 1, Int64.Type),"Custom", (x)=> x[Index]-x[RECORD_ORDER]),{"Custom"},{{"Orders",(y)=>Text.Combine(List.Transform(y[RECORD_ORDER],Text.From),",")},{"Sum Datdiff",(y)=>List.Sum(y[datediff])}})}}),
    #"Expanded sum" = Table.ExpandTableColumn(#"Grouped Rows", "sum", {"Orders", "Sum Datdiff"}, {"Orders", "Sum Datdiff"})
in
    #"Expanded sum"

 

If my answer helped solve your issue, please consider marking it as the accepted solution. It helps others in the community find answers faster—and keeps the community growing stronger!
You can also check out my YouTube channel for tutorials, tips, and real-world solutions in Power Query with the following link
https://youtube.com/@omidbi?si=96Bo-ZsSwOx0Z36h

View solution in original post

Anonymous
Not applicable

Hi,

Thank for the solutions Omid_Motamedise, dufoq3 and AlienSx offered and i want to offer some more information for user to refer to.

hello @ELOO 

You can try the following code.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Xc67DcAwCATQXahdxD9MZrG8/xqBs2UFimueuBNzUus85KVEVfPQSj9isKOh6QzLpSqfZqCmySCItQTsaA8aQeRsBWq3eGmXHeEyPFssausD", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [NUMBER = _t, RECORD_ORDER = _t, datediff = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"NUMBER", Int64.Type}, {"RECORD_ORDER", Int64.Type}, {"datediff", Int64.Type}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"NUMBER", Order.Ascending}, {"RECORD_ORDER", Order.Ascending}}),
    #"Added Custom" = Table.AddColumn(#"Sorted Rows", "Custom", each let a=[NUMBER],
b=[RECORD_ORDER],
c=Table.SelectRows(#"Sorted Rows",each [NUMBER]=a and [RECORD_ORDER]=b+1)
in if Table.IsEmpty(c)=false then [datediff]+c[datediff]{0} else null),
    #"Filled Down" = Table.FillDown(#"Added Custom",{"Custom"})
in
    #"Filled Down"

Output

vxinruzhumsft_0-1725604429493.png

 

Best Regards!

Yolo Zhu

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

View solution in original post

11 REPLIES 11
dufoq3
Super User
Super User

Hi @ELOO, check this:

 

Output:

dufoq3_0-1725534409800.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Xc67DcAwCATQXahdxD9MZrG8/xqBs2UFimueuBNzUus85KVEVfPQSj9isKOh6QzLpSqfZqCmySCItQTsaA8aQeRsBWq3eGmXHeEyPFssausD", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [NUMBER = _t, RECORD_ORDER = _t, datediff = _t]),
    ChangedType = Table.TransformColumnTypes(Source,{{"NUMBER", Int64.Type}, {"RECORD_ORDER", Int64.Type}, {"datediff", Int64.Type}}),
    GroupedRows = Table.Group(ChangedType, {"NUMBER"}, {{"Solution", each 
        [ a = Table.Sort(_, {{"RECORD_ORDER", Order.Ascending}}),
          b = Table.Group(a, {"RECORD_ORDER"}, {{"G2", each List.Sum([datediff]), type table}}, GroupKind.Local, (x,y)=> Number.From( y[RECORD_ORDER] - x[RECORD_ORDER] > 1 )),
          c = Text.Combine(List.Transform(b[G2], Text.From), ", ")
        ][c], type table}}),
    SortedRows = Table.Sort(GroupedRows,{{"NUMBER", Order.Ascending}})
in
    SortedRows

Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

ELOO
Frequent Visitor

Hi thanks solution works however there is one additional challenge, the numbers in the column number there are number with  a 6 digit format but also a "AB-2024-12345" format., what change is needed for that?

The query will work, just edit ChangedType step and set text format for [Number] column.


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

AlienSx
Super User
Super User

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content], 
    idx = Table.AddIndexColumn(Table.Sort(Source, {"NUMBER", "RECORD_ORDER"}), "idx"),
    group = Table.Group(
        idx,
        {"idx", "NUMBER", "RECORD_ORDER"}, 
        {"sum", (x) => List.Sum(x[datediff])},
        GroupKind.Local, 
        (s, c) => Number.From(
            (s[NUMBER] <> c[NUMBER]) or 
            (c[RECORD_ORDER] - s[RECORD_ORDER]) <> (c[idx] - s[idx])
        )
    )[[NUMBER], [RECORD_ORDER], [sum]]
in
    group
ELOO
Frequent Visitor

Hi thanks solution works however there is one additional challenge, the numbers in the column number there are number with  a 6 digit format but also a "AB-2024-12345" format., what change is needed for that?

Omid_Motamedise
Super User
Super User

The question is not clear for me, can you provide your desire solution?

If my answer helped solve your issue, please consider marking it as the accepted solution. It helps others in the community find answers faster—and keeps the community growing stronger!
You can also check out my YouTube channel for tutorials, tips, and real-world solutions in Power Query with the following link
https://youtube.com/@omidbi?si=96Bo-ZsSwOx0Z36h

for 123456 the solution is 7

for 234567 the solution is 11

for 345678 the solution is 11, and 52

for 456789 the solution is 23 and 59

for 

it was a great question.

use the next code to reach this result

 

Omid_Motamedise_0-1725577458348.png

 

let
    Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    #"Grouped Rows" = Table.Group(Source, {"NUMBER"}, {{"sum", each Table.Group(Table.AddColumn(Table.AddIndexColumn(Table.Sort(_,"RECORD_ORDER"), "Index", 0, 1, Int64.Type),"Custom", (x)=> x[Index]-x[RECORD_ORDER]),{"Custom"},{{"Orders",(y)=>Text.Combine(List.Transform(y[RECORD_ORDER],Text.From),",")},{"Sum Datdiff",(y)=>List.Sum(y[datediff])}})}}),
    #"Expanded sum" = Table.ExpandTableColumn(#"Grouped Rows", "sum", {"Orders", "Sum Datdiff"}, {"Orders", "Sum Datdiff"})
in
    #"Expanded sum"

 

If my answer helped solve your issue, please consider marking it as the accepted solution. It helps others in the community find answers faster—and keeps the community growing stronger!
You can also check out my YouTube channel for tutorials, tips, and real-world solutions in Power Query with the following link
https://youtube.com/@omidbi?si=96Bo-ZsSwOx0Z36h
Anonymous
Not applicable

Hi,

Thank for the solutions Omid_Motamedise, dufoq3 and AlienSx offered and i want to offer some more information for user to refer to.

hello @ELOO 

You can try the following code.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Xc67DcAwCATQXahdxD9MZrG8/xqBs2UFimueuBNzUus85KVEVfPQSj9isKOh6QzLpSqfZqCmySCItQTsaA8aQeRsBWq3eGmXHeEyPFssausD", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [NUMBER = _t, RECORD_ORDER = _t, datediff = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"NUMBER", Int64.Type}, {"RECORD_ORDER", Int64.Type}, {"datediff", Int64.Type}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"NUMBER", Order.Ascending}, {"RECORD_ORDER", Order.Ascending}}),
    #"Added Custom" = Table.AddColumn(#"Sorted Rows", "Custom", each let a=[NUMBER],
b=[RECORD_ORDER],
c=Table.SelectRows(#"Sorted Rows",each [NUMBER]=a and [RECORD_ORDER]=b+1)
in if Table.IsEmpty(c)=false then [datediff]+c[datediff]{0} else null),
    #"Filled Down" = Table.FillDown(#"Added Custom",{"Custom"})
in
    #"Filled Down"

Output

vxinruzhumsft_0-1725604429493.png

 

Best Regards!

Yolo Zhu

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

Joe_Barry
Super User
Super User

Hi @ELOO 

 

In Power Query highlight the NUMBER column, then click on Group By in Home tab in ribbon. You will get a pop up, name your column and choose SUM in the Operation field and on the column field choose column C

 

Hope this helps

Joe




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





Learn about the Star Schema, it will solve many issues in Power BI!

Date tables help! Learn more



LinkedIn
Let's connect on LinkedIn


Hi thanks for looking into this, however there are two conditions in order to sum

1. have the same record number AND

2. only sum if the record order is consecutive in column B

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.

Top Kudoed Authors