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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
UserBI_PA
New Member

Complex count first occurrence per date.

Hi magicians.
I need your wisdom to create a calculated column called 'CPU' based on the following conditions:

- Per each day ('Date' column)
- The first occurrence of Placa column
- That fulfill tow conditions:  Type colmun = 'x' and Status column = 'close' 

If everything above is true then mark '1' on 'CPU' column.

 

Example of the desired result:

 

DateTypeStatusPlacaCPU
1/01/2022xopenaaa0
3/01/2022yclosebbb0
4/01/2022ycloseaaa0
1/01/2022yopenaaa0
2/01/2022ycloseccc0
2/01/2022yopenaaa0
2/01/2022xcloseaaa1
2/01/2022xopenbbb0
3/01/2022yopenccc0
3/01/2022xcloseaaa1
3/01/2022xcloseaaa0
3/01/2022xcloseaaa0
3/01/2022xcloseccc1
4/01/2022xopenaaa0
1/01/2022yclosebbb0
2/01/2022xcloseccc1
4/01/2022yopenbbb0
4/01/2022xcloseccc1
1/01/2022xclosebbb1
1/01/2022xclosebbb0

 

I manage to do this wrapping my dataset with python script however when working with bigger data the script's process never ends.
Finally, I partially solved this problem with DAX  but only works when my visual is in days, when the context change to months everything breaks. This is my spell:

CPU = CALCULATE(
DISTINCTCOUNT(Table1[Placa]),
FILTER(Table1,Table1[Status]="close"),
FILTER(Table1,Table1[Type]=X||Table1[Type]=x)
)
 
Thank you in advance!
1 ACCEPTED SOLUTION
AlexisOlson
Super User
Super User

DAX cannot distinguish between identical rows, so you need some sort of ordering index to know which is "first". As a result, this is probably better done in the query editor (which can easily generate an index column).

 

Here's an example of what the query might look like if you did this entirely in the query editor:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtQ3MNQ3MjAyUtJRqgDi/ILUPCCVmJioFKsTrWSMJF0JxMk5+cWpQDopKQksb4JDHqbfEE0ezXgjHNqTk5OxyuPRXoHFdnR5qHaY49E9B5WGWW5MwHRaycPsN8EfNehBix41uAIHm/GVmIGDbju6dvSEg249XvlYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Type = _t, Status = _t, Placa = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Type", type text}, {"Status", type text}, {"Placa", type text}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
    #"Filtered Rows" = Table.SelectRows(#"Added Index", each ([Type] = "x") and ([Status] = "close")),
    #"Grouped Rows" = Table.Group(#"Filtered Rows", {"Date", "Placa"}, {{"Index", each List.Min([Index]), type number}}),
    #"Merged Queries" = Table.NestedJoin(#"Added Index", {"Index"}, #"Grouped Rows", {"Index"}, "Grouped Rows", JoinKind.LeftOuter),
    #"Expanded Grouped Rows" = Table.ExpandTableColumn(#"Merged Queries", "Grouped Rows", {"Index"}, {"CPU"}),
    #"Transformed Column" = Table.TransformColumns(#"Expanded Grouped Rows", {{"CPU", each if _ = null then 0 else 1, Int64.Type}})
in
    #"Transformed Column"

(Try pasting this into the Advanced Editor in a new blank query.)

 

Alternatively, you can just add the index column in the query editor and do the rest in DAX.

CPU =
VAR _MinIndex =
    CALCULATE (
        MIN ( Table1[Index] ),
        ALLEXCEPT ( Table1, Table1[Date], Table1[Placa] ),
        Table1[Status] = "close",
        Table1[Type] = "x"
    )
RETURN
    IF ( Table1[Index] = _MinIndex, 1, 0 )

View solution in original post

1 REPLY 1
AlexisOlson
Super User
Super User

DAX cannot distinguish between identical rows, so you need some sort of ordering index to know which is "first". As a result, this is probably better done in the query editor (which can easily generate an index column).

 

Here's an example of what the query might look like if you did this entirely in the query editor:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtQ3MNQ3MjAyUtJRqgDi/ILUPCCVmJioFKsTrWSMJF0JxMk5+cWpQDopKQksb4JDHqbfEE0ezXgjHNqTk5OxyuPRXoHFdnR5qHaY49E9B5WGWW5MwHRaycPsN8EfNehBix41uAIHm/GVmIGDbju6dvSEg249XvlYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Type = _t, Status = _t, Placa = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Type", type text}, {"Status", type text}, {"Placa", type text}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
    #"Filtered Rows" = Table.SelectRows(#"Added Index", each ([Type] = "x") and ([Status] = "close")),
    #"Grouped Rows" = Table.Group(#"Filtered Rows", {"Date", "Placa"}, {{"Index", each List.Min([Index]), type number}}),
    #"Merged Queries" = Table.NestedJoin(#"Added Index", {"Index"}, #"Grouped Rows", {"Index"}, "Grouped Rows", JoinKind.LeftOuter),
    #"Expanded Grouped Rows" = Table.ExpandTableColumn(#"Merged Queries", "Grouped Rows", {"Index"}, {"CPU"}),
    #"Transformed Column" = Table.TransformColumns(#"Expanded Grouped Rows", {{"CPU", each if _ = null then 0 else 1, Int64.Type}})
in
    #"Transformed Column"

(Try pasting this into the Advanced Editor in a new blank query.)

 

Alternatively, you can just add the index column in the query editor and do the rest in DAX.

CPU =
VAR _MinIndex =
    CALCULATE (
        MIN ( Table1[Index] ),
        ALLEXCEPT ( Table1, Table1[Date], Table1[Placa] ),
        Table1[Status] = "close",
        Table1[Type] = "x"
    )
RETURN
    IF ( Table1[Index] = _MinIndex, 1, 0 )

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.