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
Sriku
Helper IV
Helper IV

How to apply this logic in Power query

Hi, 

Logic.PNG

In the data I want to apply in power query . It tell how many consectuive count of red. Need help

UC IDDateOverall FlagRed Flag
C131-Jan-20Red1
C129-Feb-20Green0
C131-Mar-20Green0
C130-Apr-20Green0
C131-May-20Red1
C130-Jun-20Green0
C131-Jul-20Green0
C131-Aug-20Green0
C130-Sep-20Green0
C131-Oct-20Red1
C130-Nov-20Red2
C131-Dec-20Red3
C231-Jan-20Red1
C229-Feb-20Red2
C231-Mar-20Red3
C230-Apr-20Green0
C231-May-20Green0
C230-Jun-20Green0
C231-Jul-20Red1
C231-Aug-20Red2
C230-Sep-20Red3
C231-Oct-20Red4
C230-Nov-20Red5
C231-Dec-20Red6
1 ACCEPTED SOLUTION

Sorry @Sriku  for late reply, too busy currently.

You can use the 5th element of the Group function in combination with a nested index like so:

 

 

let
  Source = Table.FromRows(
      Json.Document( Binary.Decompress( Binary.FromText(
                  "fZDNCsIwEAbfJecGkq0KHouiEFBBjyWHWoMXiaUYoW9vpFbz4+4lDQyTfkxds5VkBSslV43lIPz9aC7+lEwXHwhLvjHnEW57Y6z/ih/27q7pcSx41RH4bQ/In72rnKVc5W4UrtyVGnYyHWUf2gc+bH9/hhAic23aEJYjBKo0JKWjZyHpnD+LV/66A47xztPmqXO2Oaycbw4a/0uRFJ5FZlJ4HplJ4QXT+gU=", BinaryEncoding.Base64 ),  Compression.Deflate ) , 
      let
        _t = ((type nullable text) meta [Serialized.Text = true])
      in
        type table[#"UC ID" = _t, Date = _t, #"Overall Flag" = _t, TargetValue = _t]
    ),

  #"Grouped Rows" = Table.Group(
      Source, 
      {"UC ID", "Overall Flag"}, 
      {{"All", each Table.AddIndexColumn(_, "GeneratedResult", 1, 1)}}, 
      GroupKind.Local, 
      (x, y) => Number.From(x <> y)
    ),
  #"Expanded All" = Table.ExpandTableColumn(
      #"Grouped Rows", 
      "All", 
      {"Date", "TargetValue", "GeneratedResult"}, 
      {"Date", "TargetValue", "GeneratedResult"}
    ),
  #"Added Custom" = Table.AddColumn(
      #"Expanded All", 
      "FinalResult", 
      each if [Overall Flag] = "Red" then [GeneratedResult] else 0
    ),
  #"Removed Columns" = Table.RemoveColumns(#"Added Custom", {"GeneratedResult"})
in
  #"Removed Columns"

 

 

Some background on how this works:

5th element:  https://www.thebiccountant.com/2018/01/21/table-group-exploring-the-5th-element-in-power-bi-and-powe... and for the nested index: https://www.youtube.com/watch?v=-3KFZaYImEY

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

9 REPLIES 9
amitchandak
Super User
Super User

@ImkeF , can you help on this

Can any one please help on this 

If not in power query. Any other method to solve this. Basically for each "UC id"  need to count how many  consequetive occurance of red

 

Sorry @Sriku  for late reply, too busy currently.

You can use the 5th element of the Group function in combination with a nested index like so:

 

 

let
  Source = Table.FromRows(
      Json.Document( Binary.Decompress( Binary.FromText(
                  "fZDNCsIwEAbfJecGkq0KHouiEFBBjyWHWoMXiaUYoW9vpFbz4+4lDQyTfkxds5VkBSslV43lIPz9aC7+lEwXHwhLvjHnEW57Y6z/ih/27q7pcSx41RH4bQ/In72rnKVc5W4UrtyVGnYyHWUf2gc+bH9/hhAic23aEJYjBKo0JKWjZyHpnD+LV/66A47xztPmqXO2Oaycbw4a/0uRFJ5FZlJ4HplJ4QXT+gU=", BinaryEncoding.Base64 ),  Compression.Deflate ) , 
      let
        _t = ((type nullable text) meta [Serialized.Text = true])
      in
        type table[#"UC ID" = _t, Date = _t, #"Overall Flag" = _t, TargetValue = _t]
    ),

  #"Grouped Rows" = Table.Group(
      Source, 
      {"UC ID", "Overall Flag"}, 
      {{"All", each Table.AddIndexColumn(_, "GeneratedResult", 1, 1)}}, 
      GroupKind.Local, 
      (x, y) => Number.From(x <> y)
    ),
  #"Expanded All" = Table.ExpandTableColumn(
      #"Grouped Rows", 
      "All", 
      {"Date", "TargetValue", "GeneratedResult"}, 
      {"Date", "TargetValue", "GeneratedResult"}
    ),
  #"Added Custom" = Table.AddColumn(
      #"Expanded All", 
      "FinalResult", 
      each if [Overall Flag] = "Red" then [GeneratedResult] else 0
    ),
  #"Removed Columns" = Table.RemoveColumns(#"Added Custom", {"GeneratedResult"})
in
  #"Removed Columns"

 

 

Some background on how this works:

5th element:  https://www.thebiccountant.com/2018/01/21/table-group-exploring-the-5th-element-in-power-bi-and-powe... and for the nested index: https://www.youtube.com/watch?v=-3KFZaYImEY

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

Thanks for your response. Can you please share the Powerbi working. Need to understand more since I am a learner 

Yes sure. 
But could also just paste the code into the advanced editor of a new query and have everything there is.

 

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

Thanks a Lot for Sharing. Let me try and check.

Is other query table are relevant to the table query or I can delete those query table

You can delete them. Meant to upload a cleaned version.

 

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.