Count streaks or consecutive repeat of values DAX - powerbi

I have a table that is quite simple:
User
Date
Status
John
May 2021
Success
Doe
May 2021
Fail
John
Aug 2021
Fail
Doe
Aug 2021
Fail
Doe
Sep 2021
Success
Doe
Oct 2021
Success
John
OCt 2021
Fail
I want to count how many times a user fails repeatedly but reset the count when it succeeds.
In the example above I would want to add a column like this :
User
Date
Status
Streak
John
May 2021
Success
0
Doe
May 2021
Fail
0
John
Aug 2021
Fail
0
Doe
Aug 2021
Fail
1
Doe
Sep 2021
Success
0
Doe
Oct 2021
Success
0
John
OCt 2021
Fail
1
Now this streak count would have to increase even if the user did not appear in a month as the example shown. I can not use power query and my main concern is the discrepancy in dates since sometimes users can have a streak since they only got tested months apart. and so on.

How about this?
Table.AddColumn(
#"Previous Step",
"Streak",
(r) => if r[Status] = "Fail"
then List.Count(
List.LastN(
Table.SelectRows(
#"Previous Step",
each [User] = r[User] and [Date] < r[Date]
)[Status],
each _ = "Fail"
)
)
else 0,
Int64.Type
)
If the [Status] is "Fail", then it will take the table from the #"Previous Step" and Table.SelectRows just the rows where the [User] is the same as in the current row and the [Date] is before the date in the current row and return just the [Status] column from that filtered table. Treating this single column as a list, it then takes the List.LastN occurrences of "Fail" from that list and does a List.Count of how many of those there are.

can you add conditional column ?
if status = success new_column = 0 , like this.

Related

Filter only MAX month figures Power BI Desktop

I have a table of values for orders per month by region that looks like this:
Orders Table
Orders (YTD)
Month
Year
1
Jan
2021
4
Feb
2021
4
Mar
2021
5
Apr
2021
14
May
2021
16
Jun
2021
17
Jul
2021
19
Aug
2021
22
Sep
2021
24
Oct
2021
34
Nov
2021
35
Dec
2021
1
Jan
2022
3
Feb
2022
4
Mar
2022
Along with a table that orders the months in sequence as below, that will be modelled to order the months in the first table so that they appear in sequence in graphs.
Monthly Sequence Table
Month Sequence
Month
1
Jan
2
Feb
3
Mar
4
Apr
5
May
6
Jun
7
Jul
8
Aug
9
Sep
10
Oct
11
Nov
12
Dec
Upon closer inspection of my data, I have realised that the number of orders per month are not the raw figure per month, but a cumulative total for every order in the calendar year so far (new orders for month + orders for preceding month). Firstly, I want to calculate the correct sum of orders per year, which should actually just be the MAX month from the orders table. Of course in most years this will be December, but for the current year it needs to be the latest month. I wanted to use a measure to calculate the MAX 'Monthly Sequence Table'[Month Sequence] number from each table, by year. I thought maybe a filter function would be used but could not work out exactly how to do this in DAX.
Secondly, and similarly, I want to calculate the actual number of orders per each individual month using DAX. In this case, I want to take the Orders (YTD) total for that month/year combination and subtract it from its immediately preceding month. What would the formula look like for this?
Thanks in advance.

Power Query/DAX to calculate monthly raw sales figure

Dear stackoverflow, please help!
I'm hoping for some assistance with data processing in Power BI, either using Power Query or DAX. At this point I am really stuck and can't figure out how to solve this problem.
The below table is a list of sales by Product, Month, and Year. The problem with my data is that the value in the sales data is actually cumulative, rather than the raw figure of sales for that month. In other words, the figure is the sum of the number of sales for the month (for that Year and Product combination) and the number of sales for the preceding month. As you will see in the table below, the number gets progressively larger in each category as the year progresses. The true number of sales for TVs in Feb of 2021, for example, is the sales figure of 3 minus the corresponding figure for sales of TVs in Jan of 2021 (1).
I really would appreciate if anyone knows of a solution to this problem. In reality, my table has hundreds of thousands of rows, so I cannot do the calculations manually.
Is there a way to use Power Query or DAX to create a calculated column with the Raw Sales figure for each month? Something that would check if Product and Year are equal, then subtract the Jan figure from the Feb figure and so on?
Any help will be very much appreciated,
Sales Table
Product
Sales (YTD)
Month
Year
TV
1
Jan
2021
Radio
4
Jan
2021
Cooker
5
Jan
2021
TV
3
Feb
2021
Radio
5
Feb
2021
Cooker
6
Feb
2021
TV
3
Mar
2021
Radio
6
Mar
2021
Cooker
8
Mar
2021
TV
5
Apr
2021
Radio
7
Apr
2021
Cooker
8
Apr
2021
TV
7
May
2021
Radio
8
May
2021
Cooker
8
May
2021
TV
9
Jun
2021
Radio
10
Jun
2021
Cooker
10
Jun
2021
TV
10
Jul
2021
Radio
10
Jul
2021
Cooker
10
Jul
2021
TV
11
Aug
2021
Radio
13
Aug
2021
Cooker
12
Aug
2021
TV
11
Sep
2021
Radio
13
Sep
2021
Cooker
12
Sep
2021
TV
12
Oct
2021
Radio
14
Oct
2021
Cooker
13
Oct
2021
TV
17
Nov
2021
Radio
19
Nov
2021
Cooker
17
Nov
2021
TV
19
Dec
2021
Radio
20
Dec
2021
Cooker
20
Dec
2021
TV
4
Jan
2022
Radio
2
Jan
2022
Cooker
3
Jan
2022
TV
5
Feb
2022
Radio
3
Feb
2022
Cooker
5
Feb
2022
Thanks, Jim
Give this a try in powerquery / M. It groups on Product and Year, then sorts the months, and subtracts each row from the next row to determine the period amount.
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Grouped Rows" = Table.Group(Source, {"Product", "Year"}, {
{"data", each
let r=Table.Sort(Table.AddIndexColumn(_, "Index", 0, 1),{ each List.PositionOf({"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}, [Month]), {"Month",Order.Ascending}}),
x= Table.AddColumn( r, "Period Sales", each if [Index]=0 then [#"Sales (YTD)"] else [#"Sales (YTD)"]-r{[Index]-1}[#"Sales (YTD)"])
in x
, type table }
}),
#"Expanded data" = Table.ExpandTableColumn(#"Grouped Rows", "data", {"Sales (YTD)", "Month", "Period Sales"}, {"Sales (YTD)", "Month", "Period Sales"})
in #"Expanded data"

PowerBI - Counting the attributes of a record based on date

I have two simple tables. I need to able to determine who is 'new' as of a particular date (say January) and then count only those attributes. There's a 1:M relationship on name. I basically need to answer the following questions with the below data:
What is the total number of FamilyMembers based on log-in for the month? (Done using custom measure)
Out of the total of #1 - how many have logged in for the first time?
Out of the total of #2 - how many were children? How many were adults?
Log In Table
ID
Name
Date
login1
Sam
Jan
login2
Sam
Jan
login3
Dave
Jan
login4
Dave
Jan
login5
Jack
Jan
login6
Sam
Jan
login7
James
Feb
login8
James
Feb
login9
James
Feb
login10
Sam
Feb
login11
Sam
Feb
login12
Steve
Feb
Contact Table
Name
FamilyMembers
Child
Adult
Sam
3
1
2
James
2
1
1
Dave
4
2
2
Jack
1
0
1
Steve
6
1
5
Using this data, filtered on February we would see Steve never signed in prior to that date, so that makes him 'new'. James is also new.
My closest attempt is the custom 'Count of New Individuals' Measure
VAR currentUsers = VALUES('Log-Ins'[Name])
VAR currentDate = MIN('Log-Ins'[Date])
VAR pastUsers = CALCULATETABLE(VALUES('Log-Ins'[Name]),
ALL('Log-Ins'[Date].[Month],'Log-Ins'[Date].[MonthNo],'Log-Ins'[Date].[Year])
, 'Log-Ins'[Date]<currentDate)
VAR newUsers = EXCEPT(currentUsers,pastUsers)
RETURN COUNTROWS(newUsers)
As you can see this gives me the count of new individuals but I want to count their attributes to say :: Out of the 11 total family members, 8 were new. Out of those 8, 6 were adults and 2 were children.
I may be getting lost in the translation, but I don't understand how exactly you want to display the information.
#ContactsWhoLoggedIN :=
CALCULATE(COUNTROWS(Contacts),FILTER(Contacts,CALCULATE(COUNTROWS(LogIN)>0)))
#NewCWhoLoggedIN :=
CALCULATE(COUNTROWS(Contacts),
FILTER(Contacts,
//LoggedIn in the Current Date Context
CALCULATE(COUNTROWS(LogIN))>0
&&
//Never LoogedIN before the Current Date Context
CALCULATE(COUNTROWS(LogIN),FILTER(ALL(Dates),Dates[Date]<MIN(Dates[Date])))=0
)
)
#CWhoLoggedBackIN := [#ContactsWhoLoggedIN]-[#NewCWhoLoggedIN]
#FM_NewCWLI :=
CALCULATE(SUM(Contacts[FamilyMembers]),
FILTER(Contacts,
//LoggedIn in the Current Date Context
CALCULATE(COUNTROWS(LogIN))>0
&&
//Never LoogedIN before the Current Date Context
CALCULATE(COUNTROWS(LogIN),FILTER(ALL(Dates),Dates[Date]<MIN(Dates[Date])))=0
)
)
I remember this pattern from "Microsoft Excel 2013: Building Data Models with PowerPivot"

How to count number of projects that got approved or the status changed in DAX

I have a data set that looks like below. The data is about project ideas that are raised in our company. The project ideas will be kept in Backlog section first and then moved to Do Next once then get approved. The modified date is a date when the project idea reviewed and approved or the status changed to Do Next. If it is not approved, then it remains in Backlog.
Id Modified Date Created Date Status
1 8/4/2017 8/4/2017 Backlog
2 6/10/2021 6/10/2021 Backlog
3 3/5/2018 3/5/2018 Backlog
1 6/5/2020 8/4/2017 Do Next
1 30/5/2021 3/5/2020 Analyze
2 5/4/2022 6/10/2021 Do Next
2 8/8/2022 6/10/2021 Analyze
What I want to achieve is that, I want to count how many projects moved from Backlog to Do Next status using DAX function or approved. So this time, for example I have two projects that moved from Backlog to Do Next (Id1 and Id 2). I am relatively new to DAX. Can anyone please help me on this?
I have modified your table to add an "orphan" Do Next Status, as the last row, to test the count dependency on the previous Status Backlog.
Assuming your table look like this
Table
Id
Modified Date
Created Date
Status
1
08 April 2017
08 April 2017
Backlog
2
06 October 2021
06 October 2021
Backlog
3
03 May 2018
03 May 2018
Backlog
1
06 May 2020
08 April 2017
Do Next
1
30 May 2021
03 May 2020
Analyze
2
05 April 2022
06 October 2021
Do Next
2
08 August 2022
06 October 2021
Analyze
4
09 August 2022
06 October 2021
Do Next
Calculation
Count Projects =
VAR BacklogProjects =
SUMMARIZE ( FILTER ( 'Table', [Status] = "Backlog" ), [Id] )
VAR DoNextProjects =
SUMMARIZE ( FILTER ( 'Table', [Status] = "Do Next" ), [Id] )
VAR SameProjects =
INTERSECT ( BacklogProjects, DoNextProjects )
VAR Result =
COUNTROWS ( SameProjects )
RETURN
Result
``

Calculate active contracts based on start and end date in Power BI?

My Data looks something like this:
ContractID Start Date End Date
1 01.01.2020 23.03.2020
2 15.02.2020 29.07.2020
3 06.06.2020 null
The last contract would be still active. I have a DateTable with the Start Date as the active relationship.
I need the end result too look like this:
Date Active Contracts
Jan 1
Feb 2
Mar 2
Apr 1
May 1
Jun 2
How should the measure look like?
Thanks in advance!
Assuming you have a month in your date table
VAR currentMonth = SELECTEDVALUE(MyDataTable[Month value]) --needs to be a number 1 to 12
RETURN CALCULATE(COUNTROWS(MyDataTable),
ALL(DateTable),
MONTH(MyDataTable[Start Date]) >= currentMonth,
ISBLANK(MyDataTable[End Date]) || MONTH(MyDataTable[End Date]) <= currentMonth)