Counting previous occurrences in Dax

5foot9

New Member
Joined
Nov 19, 2013
Messages
15
[FONT=&quot]I have a column called 'slots' containing values from 1 to 100 which populate the column any amount of times and in any order. They are sorted by date but I don't know if it's possible to use EARLIER to count previous occurrences of each slot by row as there are often multiple occurrences for each date, ideally I would like to avoid relying on dates. [/FONT]

[FONT=&quot]So I am trying to count each occurrence of a slot number row by row in a calculated column to eventually calculate sequences and frequencies.[/FONT]

[FONT=&quot]This is what I've been trying but gives me the total count of each slot number each time. If anyone is able to point me in the right direction it would be greatly appreciated.[/FONT]

[FONT=&quot][=CALCULATE(COUNT([Slot]),FILTER(Records,Records[Slot]<=EARLIER(Records[Slot])))]

[/FONT]
I've posted the same post here too - PowerPivot Forum - Login

By the way I'm using powerpivot 2016
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
You literally want to look at the rows above the current row in the table? As far as I am aware PowerPivot doesn't even consider the position of a row.

In order to do what you want to do you would need to add an index column to you data. You could easily do that with Power Query if needed. Then use your filter logic above with earlier to pick up the row above.

NB that you probably only want an = in the part above to look for rows with the same value in the slot column.
 
Upvote 0
I had a quick look and he mentions that you can use EARLIER to look at the previous row, but does not show how to actually do it. His first link is just a description of EARLIER and the second is a link to his book...

You could just add an index column in excel and then bring that into PP. The Power Query way is pretty simple and I could explain that if it helped.

Once you have an index column you want something like.

Code:
CALCULATE ( 
    COUNTROWS ( Records ),
    FILTER ( ALL ( Records ),
        [COLOR=#333333]Records[Slot]=EARLIER ( Records[Slot] ) &&
        Records[Index] < EARLIER ( Records[Index]
    )
)
[/COLOR]
 
Upvote 0
Ok....I went round in circles with this late last night and gave up!....
I think sometimes when I'm faced with so much information I can't see the wood for the trees!

I added an index in excel and entered your DAX, I had to make'<=' as the original was not in the correct row. The problem I found then was it was picking total count of slot, not just losers which is what I actually want! I've added that in as you can see below and ideally I would like the blanks populated with 0 but what its doing now is just counting all the losers and not giving me streaks or sequences.

Code:
=CALCULATE (     COUNTROWS ( Records ),
    FILTER ( ALL ( Records ),
        Records[Slot]=EARLIER ( Records[Slot] ) &&
        EARLIER(Records[Lost])=1 && Records[Index] <= EARLIER ( Records[Index]
    )
))

I have made this work in an excel table where the result triggers a reset in the sequence - I don't know if this is possible with PP.
I don't have permission to post attachments unfortunately hopefully this will do -

EXCELPOWER PIVOT
DateIDslotcount of slotLostLossSeqDateindexslotLostSeqTest
12/04/201711-370113701312/04/20178557111370
18/04/201711-371113710018/04/20178615110
19/04/201711-372113721119/04/20178625111372
19/04/201711-373113731219/04/20178626111373
19/04/201711-374113741319/04/20178627111374
19/04/201711-375113751419/04/20178628111375
20/04/201711-376113761520/04/20178633111376
20/04/201711-377113771620/04/20178634111377
21/04/201711-378113781721/04/20178639111378
22/04/201711-379113791822/04/20178652111379
22/04/201711-380113801922/04/20178653111380
25/04/201711-381113810025/04/20178674110
29/04/201711-382113821129/04/20178706111382
30/04/201711-383113831230/04/20178707111383
30/04/201711-384113841330/04/20178708111384
01/05/201711-385113850001/05/20178716110
01/05/201711-386113861101/05/20178717111386
03/05/201711-387113871203/05/20178727111387
03/05/201711-388113881303/05/20178728111388
05/05/201711-389113891405/05/20178739111389

<colgroup><col width="75" style="width:56pt"> <col width="64" style="width:48pt" span="2"> <col width="99" style="width:74pt"> <col width="64" style="width:48pt"> <col width="71" style="width:53pt" span="2"> <col width="75" style="width:56pt"> <col width="64" style="width:48pt" span="4"> </colgroup><tbody>
</tbody>
 
Upvote 0
You can make a measure that returns a blank return a 0 instead by simply adding + 0 to it.
 
Upvote 0

Forum statistics

Threads
1,217,388
Messages
6,136,311
Members
450,003
Latest member
AnnetteP

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top