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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
LexN
Frequent Visitor

Count each occurence based on a timeframe

Hello, everyone

 

I need some help with a formula that is above my current knowledge. If anyone could point me to the right direction, it would be much appreciated. 

 

Bassically I need to have a calculated column with the number of each iterration from a certain timeframe (5 days from the oldest date in that batch). On top of this, for cases where we have only 1 iterration in the 5 days timeframe, we should have "0" on the CaseCount column.

 

I tried various formulas, mostly using EARLIER, but I encountered some issues, the formula became too messy and I decided to drop it, now I am testing some other formulas using VAR, but with no succes.

 

I am attaching a table with minimum data, and different scenarios. The formula in the CaseCount column is:

 

 

 

CaseCount = 
VAR TimeLimit = 5
VAR UserID = DataSource[UserID]
VAR SubjectCode = DataSource[SubjectCode]
VAR CaseDate = DataSource[CreationDate]
VAR Exceptions = DataSource[Status]

VAR CaseCount=CALCULATE(COUNTROWS(DataSource),
    FILTER(DataSource, 
        UserID = DataSource[UserID] && 
        SubjectCode = DataSource[SubjectCode] && 
        CaseDate >= DataSource[CreationDate] && 
        Exceptions = "Valid"
))

RETURN CaseCount

 

 

 

SubjectCodeCreationDateUserIDStatusCaseCountDesired CaseCount
11112/4/2022 11:48111111Valid11
11112/5/2022 16:29111111Valid22
11112/5/2022 17:39111111Valid33
11112/10/2022 12:07111111Valid41
11112/13/2022 13:09111111Valid52
11112/14/2022 14:05111111Invalid  
11112/14/2022 19:48111111Valid73
11112/15/2022 09:05111111Valid84
11112/15/2022 16:03111111Valid90
22222/4/2022 11:48111111Valid11
22222/5/2022 16:29111111Valid22
22222/5/2022 17:39111111Valid33
33332/4/2022 11:48111111Valid11
33332/5/2022 16:29111111Valid22
33332/5/2022 17:39111111Valid33
33332/10/2022 12:07111111Valid40
33332/15/2022 13:09111111Valid51
33332/16/2022 19:48111111Valid62
33332/17/2022 09:05111111Valid73
33332/17/2022 16:03111111Valid84
44442/4/2022 11:48111111Valid10
44442/15/2022 16:29111111Valid20
11112/5/2022 11:48222222Valid11
11112/6/2022 16:29222222Valid22
11112/6/2022 17:39222222Valid33
11112/11/2022 12:07222222Valid41
11112/14/2022 13:09222222Valid52
11112/15/2022 14:05222222Invalid  
11112/15/2022 19:48222222Valid73
11112/16/2022 09:05222222Valid84
11112/16/2022 16:03222222Valid90
22222/5/2022 11:48222222Valid11
22222/6/2022 16:29222222Valid22
22222/6/2022 17:39222222Valid33
33332/5/2022 11:48222222Valid11
33332/6/2022 16:29222222Valid22
33332/6/2022 17:39222222Valid33
33332/11/2022 12:07222222Valid40
33332/16/2022 13:09222222Valid51
33332/17/2022 19:48222222Valid62
33332/18/2022 09:05222222Valid73
33332/18/2022 16:03222222Valid84
44442/5/2022 11:48222222Valid10
44442/16/2022 16:29222222Valid20

 

As I said, any help about how this can be done would be a life saver.

 

Thank you

3 REPLIES 3
v-kkf-msft
Community Support
Community Support

Hi @LexN ,


I am not sure if I understood your question correctly. Please check the following calculated columns to see if they meet expectations.

 

Is_0_Case = 
VAR TimeLimit = 5
VAR UserID = DataSource[UserID]
VAR SubjectCode = DataSource[SubjectCode]
VAR CaseDate = DataSource[CreationDate]
VAR nextdate =
    CALCULATE (
        MIN ( DataSource[CreationDate] ),
        FILTER (
            DataSource,
            UserID = DataSource[UserID]
                && SubjectCode = DataSource[SubjectCode]
                && CaseDate < DataSource[CreationDate]
                && DataSource[Status] = "Valid"
        )
    )
VAR diff =
    DATEDIFF ( [CreationDate], nextdate, HOUR ) / 24
RETURN
    IF ( diff >= 5 || diff = BLANK (), "0" )
CaseCount_output = 
VAR last_0 =
    CALCULATE (
        MAX ( [CreationDate] ),
        FILTER (
            DataSource,
            DataSource[SubjectCode] = EARLIER ( DataSource[SubjectCode] )
                && DataSource[CreationDate] <= EARLIER ( DataSource[CreationDate] )
                && [UserID] = EARLIER ( DataSource[UserID] )
                && DataSource[Is_0_Case] = "0"
        )
    )
VAR _count =
    COUNTROWS (
        FILTER (
            DataSource,
            DataSource[SubjectCode] = EARLIER ( DataSource[SubjectCode] )
                && DataSource[CreationDate] <= EARLIER ( DataSource[CreationDate] )
                && [CreationDate] > last_0
                && [UserID] = EARLIER ( DataSource[UserID] )
        )
    )
RETURN
    IF ( DataSource[Is_0_Case] = "0", 0, _count )

vkkfmsft_0-1655088581950.png

 

If the problem is still not resolved, please provide detailed error information or the expected result you expect. Let me know immediately, looking forward to your reply.
Best Regards,
Winniz
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi, 

Thank you for looking into this. Your results are prety close to what is desired. But still, the output has the same issue that I previously encountered. The count doesn't reset when that time limit of 5 days is reached. 

 

I don't know if I am explaining this right, but, for example, the first 3 rows in the table should be included in the first group because their creation dates are within the 5 days time limit. Next, the 4th row should be the first case in a new group, because the time diff between it's creation date and the creation date of the first case in the first group is over 5 days, and so on.

 

The count on the Desired CaseCount column is what I am trying to achieve. 

The main obstacle is that I cannot find a way to reset the case counter when the difference between the current creation date and the creation date of the first case in the previous group is over 5 days.

 

Thank you again for your effort, maybe now I explained the problem better and you have the right information to build upon.

Hi @LexN ,

 

Please open a blank query and paste the code.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("lZRLagMxEESvYmZtcLe+M7pBLpCN8SLgjSFkmfNHFrJsqStWj6A3Mzz6U0WdzwvntxwXc3InQ8YcmJNb8wfm+ufz6/t2vX8odTm+IL4iIZkNIaYURGKyELGlOoSpMiZRRIyTk7GtjE0E+3g5Gj8u4BL5V+bj57dSh1KY2v65WwQb1SvQNnR6MGvZCjL52GQRc1+TCmPy2ylpQ/SSCmQuqc1v52AN0Q8mkB2DqbxGA+MVXhu24TBxTZDrcJy4JoJ94sQ1T6e5/NTiUI+wRh3CSVDbGFMN9SZvQt9FICBvQu8BgaC84d4DgkF543oPCAblje/zpjHv88b3zhGdUN6E3jmCQXkTeucIBuSNXtKG6CUVyFxSkQTzwRqiH0wgOwZTeW3Mm6Dw2pg3ceIalDfrxDUob9aJa0DeaMQZ80ajTmYufw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [SubjectCode = _t, CreationDate = _t, UserID = _t, Status = _t, CaseCount = _t, #"Desired CaseCount" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"SubjectCode", Int64.Type}, {"CreationDate", type datetime}, {"UserID", Int64.Type}, {"Status", type text}, {"CaseCount", Int64.Type}, {"Desired CaseCount", Int64.Type}}),
    #"Inserted Date" = Table.AddColumn(#"Changed Type", "Date", each DateTime.Date([CreationDate]), type date),
    #"Sorted Rows" = Table.Sort(#"Inserted Date",{{"SubjectCode", Order.Ascending}, {"UserID", Order.Ascending}, {"Date", Order.Ascending}}),
    #"Grouped Rows" = Table.Group(#"Sorted Rows", {"SubjectCode", "UserID"}, {{"allrowtab", each _, type table [SubjectCode=nullable number, CreationDate=nullable datetime, UserID=nullable number, Status=nullable text, CaseCount=nullable number, Desired CaseCount=nullable number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "StartDate", each let  tab = Table.SelectRows([allrowtab], each [Status] = "Valid"), cdate = Table.SelectColumns(tab, "Date")

in 

List.Generate(()=>[x=0,y=Record.Field(cdate{0}, "Date"),w=1],each [w] > 0,each [z=[y], x=Table.SelectRows(tab,each [Date]>Date.AddDays(z,5))[Date],y=x{0},w=List.Count(x)
],
each [y])),
    #"Expanded allrowtab" = Table.ExpandTableColumn(#"Added Custom", "allrowtab", {"CreationDate", "Status", "CaseCount", "Desired CaseCount","Date"}, {"CreationDate", "Status", "CaseCount", "Desired CaseCount","Date"}),
    #"Added Custom1" = Table.AddColumn(#"Expanded allrowtab", "IsStartDate", each if List.Contains([StartDate], [Date]) then 0 else null)
in
    #"Added Custom1"

vkkfmsft_0-1655977483240.png

 

Then create the calculate column.

 

CaseCount_output = 
VAR last_0 =
    CALCULATE (
        MAX ( [CreationDate] ),
        FILTER (
            'DataSource (3)',
            'DataSource (3)'[SubjectCode] = EARLIER ( [SubjectCode] )
                && 'DataSource (3)'[CreationDate] <= EARLIER ( [CreationDate] )
                && 'DataSource (3)'[UserID] = EARLIER ( [UserID] )
                &&  'DataSource (3)'[IsStartDate] = "0"
        )
    )
VAR _count =
    COUNTROWS (
        FILTER (
            'DataSource (3)',
            'DataSource (3)'[SubjectCode] = EARLIER ( [SubjectCode] )
                && 'DataSource (3)'[CreationDate] <= EARLIER ( [CreationDate] )
                && [CreationDate] >= last_0
                && [UserID] = EARLIER ( [UserID] )
                && [Status] = "Valid"
        )
    )
RETURN
    IF ( [Status] = "Invalid", BLANK(), _count )

vkkfmsft_1-1655977519807.png

 

If the problem is still not resolved, please provide detailed error information or the expected result you expect. Let me know immediately, looking forward to your reply.
Best Regards,
Winniz
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

Power BI Carousel June 2024

Power BI Monthly Update - June 2024

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

RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

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