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
mithrandir
Helper I
Helper I

Is there a way to increment/decrement a column value based on conditions?

I have a unique problem i've been trying to solve and haven't found anything similar to apply yet. Essentially, I have a table of events that I want to count and reset based on a condition. In the example below, the "Event Count" column is what I would like to create.

KeyEventEvent Count
Atrue1
Atrue2
Afalse0
Bfalse0
Btrue1


You can see that for every Key, I want to increment a count of true Event values, but also reset to 0 every time there is an occurence of false.

It's probably useful to know that each event has a timestamp in my data and I have already sorted and added an index to have the ability to query the data chronologically. The problem I'm having is finding a way in either Power Query or DAX to have a column reference itself. Is there a way to accomplish what I'm trying to do?

1 ACCEPTED SOLUTION
v-jiascu-msft
Employee
Employee

Hi @mithrandir,

 

Please add the following formula as a calculated column.

 

Column = var currentIndex = [Index] 
var lastFalseIndex = CALCULATE(max(Table1[Index]),
Table1[Event] = FALSE(),
Table1[Index] <= currentIndex)
var minIndexEachKey = CALCULATE(min(Table1[Index]),
all('Table1'[Event], Table1[Index]))
return
if(ISBLANK(lastFalseIndex),
currentIndex - minIndexEachKey + 1 ,
currentIndex - lastFalseIndex)

is there a way to.PNG

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Best Regards,

Dale

Community Support Team _ Dale
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

5 REPLIES 5
Ashish_Mathur
Super User
Super User

Hi,

 

Write this Query in the Query Editor

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUQoJCnVVitXBwnFz9AmG8JwweBCFsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Key = _t, Event = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Key", type text}, {"Event", type logical}}),
    #"Inserted Merged Column" = Table.AddColumn(#"Changed Type", "Merged", each Text.Combine({[Key], Text.From([Event], "en-US")}, ""), type text),
    Partition = Table.Group(#"Inserted Merged Column", {"Merged"}, {{"Partition", each Table.AddIndexColumn(_, "Index",1,1), type table}}),
    #"Expanded Partition" = Table.ExpandTableColumn(Partition, "Partition", {"Key", "Event", "Index"}, {"Key", "Event", "Index"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Partition",{"Merged"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Removed Columns",{{"Index", Int64.Type}, {"Event", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type1", "Custom", each if [Event]="false" then 0 else [Index]),
    #"Removed Columns1" = Table.RemoveColumns(#"Added Custom",{"Index"}),
    #"Uppercased Text" = Table.TransformColumns(#"Removed Columns1",{{"Event", Text.Upper, type text}})
in
    #"Uppercased Text"

 

I am sure there is a much better way that @ImkeF can suggest.

 

Here is the result i got

 

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

@Ashish_MathurPutting the values into their own tables is a clever approach, however, if I add to the data a little then you can see that the Table.Group() is combining the logical values in an undesired way.

let
    Source = Table.FromRecords({[Key="A", Event="True"],[Key="A", Event="True"],[Key="A", Event="False"],[Key="A", Event="True"],[Key="A", Event="False"],[Key="B", Event="False"],[Key="B", Event="True"],[Key="B", Event="True"]}),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Key", type text}, {"Event", type logical}}),
    #"Inserted Merged Column" = Table.AddColumn(#"Changed Type", "Merged", each Text.Combine({[Key], Text.From([Event], "en-US")}, ""), type text),
    Partition = Table.Group(#"Inserted Merged Column", {"Merged"}, {{"Partition", each Table.AddIndexColumn(_, "Index",1,1), type table}}),
    #"Expanded Partition" = Table.ExpandTableColumn(Partition, "Partition", {"Key", "Event", "Index"}, {"Key", "Event", "Index"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Partition",{"Merged"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Removed Columns",{{"Index", Int64.Type}, {"Event", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type1", "Custom", each if [Event]="false" then 0 else [Index]),
    #"Removed Columns1" = Table.RemoveColumns(#"Added Custom",{"Index"}),
    #"Uppercased Text" = Table.TransformColumns(#"Removed Columns1",{{"Event", Text.Upper, type text}})
in
    #"Uppercased Text"


I've been playing with your idea of ear-marking the false values, but before the Table.Group() step in order to keep the data separate; no luck yet. Either way, kudos for the reply and a potential solution in M.

Hi,

 

I understand mine is a rather clunky way to solve the problem.  Hope someone else can suggest a better way to do this in the Query Editor.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
v-jiascu-msft
Employee
Employee

Hi @mithrandir,

 

Please add the following formula as a calculated column.

 

Column = var currentIndex = [Index] 
var lastFalseIndex = CALCULATE(max(Table1[Index]),
Table1[Event] = FALSE(),
Table1[Index] <= currentIndex)
var minIndexEachKey = CALCULATE(min(Table1[Index]),
all('Table1'[Event], Table1[Index]))
return
if(ISBLANK(lastFalseIndex),
currentIndex - minIndexEachKey + 1 ,
currentIndex - lastFalseIndex)

is there a way to.PNG

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Best Regards,

Dale

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

@v-jiascu-msftThis is a super clean approach in DAX. Thanks for the reply!

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.