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
Anonymous
Not applicable

How do I create a variable modulo value?

I am trying to achieve a result similar to the modulo function, however the number of rows between "zeros" can vary.

 

The data I have is the TYPE column in the table below

The result I am trying to get, is the red MODULO column, where each row containing the TYPE 'Document_Header' will have a value of 0, and each row containing the TYPE 'Document_Detail' will have a sequential number which resets after each 0

 

TYPEMODULO
Document_Header0
Document_Detail1
Document_Detail2
Document_Detail3
Document_Header0
Document_Detail1
Document_Header0
Document_Detail1
Document_Detail2
Document_Detail3
Document_Detail4
Document_Header0
Document_Detail1
Document_Detail2

 

Thanks in advance for anybody that can point me in the right direction

1 ACCEPTED SOLUTION
ImkeF
Super User
Super User

Hi @Anonymous ,

please paste this code into the advanced editor and follow the steps:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCokMcFXSUfL1dwn18VeK1YlWcslPLs1NzSuJ90hNTEktAkoaoIq7pJYkZuYAxQ1xiBvhEDemzHxauwcubkI9e2MB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"(blank)" = _t, #"(blank).1" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"(blank)", type text}, {"(blank).1", type text}}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars=true]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"TYPE", type text}, {"MODULO", Int64.Type}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type1", "Index", 0, 1, Int64.Type),
    AddModIndex = Table.AddColumn(#"Added Index", "ModIndex", each if [TYPE] = "Document_Header" then [Index] else null),
    FillDownModIndex = Table.FillDown(AddModIndex,{"ModIndex"}),
    AddDynamicModulo = Table.AddColumn(FillDownModIndex, "DynamicModulo", each [Index] - [ModIndex], type number)
in
    AddDynamicModulo

What it does is to

  1. Add an index column
  2. Check if the index belongs to a header row and if so, take that index
  3. Fill down header indices down to detail rows
  4. Retrieve dynamic moduly by subtracting header index from running index
  5.  

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

1 REPLY 1
ImkeF
Super User
Super User

Hi @Anonymous ,

please paste this code into the advanced editor and follow the steps:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCokMcFXSUfL1dwn18VeK1YlWcslPLs1NzSuJ90hNTEktAkoaoIq7pJYkZuYAxQ1xiBvhEDemzHxauwcubkI9e2MB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"(blank)" = _t, #"(blank).1" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"(blank)", type text}, {"(blank).1", type text}}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars=true]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"TYPE", type text}, {"MODULO", Int64.Type}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type1", "Index", 0, 1, Int64.Type),
    AddModIndex = Table.AddColumn(#"Added Index", "ModIndex", each if [TYPE] = "Document_Header" then [Index] else null),
    FillDownModIndex = Table.FillDown(AddModIndex,{"ModIndex"}),
    AddDynamicModulo = Table.AddColumn(FillDownModIndex, "DynamicModulo", each [Index] - [ModIndex], type number)
in
    AddDynamicModulo

What it does is to

  1. Add an index column
  2. Check if the index belongs to a header row and if so, take that index
  3. Fill down header indices down to detail rows
  4. Retrieve dynamic moduly by subtracting header index from running index
  5.  

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

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.

Top Solution Authors
Top Kudoed Authors