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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
Anonymous
Not applicable

power query group by consecutive numbers

Hi, I need to group by consecutive numbers (groups of 3), starting from the latest (in this example the first group would be 34,33,32), I need to named the group as the latest number.

 

pisanoagus_0-1611687832146.png

 

This list is going to get bigger over the future months.

 

How can I achieve this in Power Query?

1 ACCEPTED SOLUTION
Icey
Community Support
Community Support

Hi @Anonymous ,

 

Try this:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Tc/JCcAwEEPRXuZs8CxeazHpv42EaEC+PnTQP0dcXeuqJkVayFNSJsQpA2KUDlFK+yU2JSCL4pBJMchIsV0tR/2iXLWLFBSkDXEK0sIoSAulIM2/288L", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Month = _t, Index = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Month", type date}, {"Index", Int64.Type}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Index", Order.Descending}}),
    #"Add Index Group" = Table.Group(#"Sorted Rows","Index",{"IndexGroup",each _},0,(x,y)=>Number.From(Number.Mod(y,3)=1)),
    #"Expanded IndexGroup" = Table.ExpandTableColumn(#"Add Index Group", "IndexGroup", {"Month", "Index"}, {"Month", "Index.1"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Expanded IndexGroup",{{"Index", Int64.Type}, {"Month", type date}, {"Index.1", Int64.Type}})
in
    #"Changed Type1"

index group.JPG

 

Note: it is needed to sort the index column by Descending.

 

 

Best regards

Icey

 

If this post helps, then consider Accepting it as the solution to help other members find it faster.

 

View solution in original post

4 REPLIES 4
Icey
Community Support
Community Support

Hi @Anonymous ,

 

Try this:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Tc/JCcAwEEPRXuZs8CxeazHpv42EaEC+PnTQP0dcXeuqJkVayFNSJsQpA2KUDlFK+yU2JSCL4pBJMchIsV0tR/2iXLWLFBSkDXEK0sIoSAulIM2/288L", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Month = _t, Index = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Month", type date}, {"Index", Int64.Type}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Index", Order.Descending}}),
    #"Add Index Group" = Table.Group(#"Sorted Rows","Index",{"IndexGroup",each _},0,(x,y)=>Number.From(Number.Mod(y,3)=1)),
    #"Expanded IndexGroup" = Table.ExpandTableColumn(#"Add Index Group", "IndexGroup", {"Month", "Index"}, {"Month", "Index.1"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Expanded IndexGroup",{{"Index", Int64.Type}, {"Month", type date}, {"Index.1", Int64.Type}})
in
    #"Changed Type1"

index group.JPG

 

Note: it is needed to sort the index column by Descending.

 

 

Best regards

Icey

 

If this post helps, then consider Accepting it as the solution to help other members find it faster.

 

parry2k
Super User
Super User

@Anonymous so your example is a bit confusing, so the latest value is 43 and you want to take that as a base and group backward in groups of 3, is this what you are after?



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

parry2k
Super User
Super User

@Anonymous what is the logic to group 32/33/34 together? What is the pattern?



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Anonymous
Not applicable

Hi, the logic is that I need to create dynamic quarters. I need to group those months by three , starting from the latest. 

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel

Power BI Monthly Update - May 2024

Check out the May 2024 Power BI update to learn about new features.