Power BI - Matrix count of values in query - powerbi

I am trying to create a matrix table that will count the number of jobs due in a month and also if they are completed on time.
At present I can get it this far.
I have tried summarizing data, creating measures etc. But to no avail.
I need to produce the following:
My data source is a series of transactions lines that include
1) Transaction Due date
2) Transaction completed date
Definition of terms:
Due in month Count of due date
Done on time Completed within due month
Outside time completed outside due month
"%Perf" percentage completed on time.
Any help with this would be much appreciated.

Add the following measures:
Measure "Due":
Due = COUNTROWS ( Table1 )
Measure "Done on time":
Done on time =
VAR DueMonth = MONTH ( FIRSTDATE ( Table1[due_date] ) )
RETURN
CALCULATE (
[Due],
FILTER (
Table1,
MONTH ( Table1[completed] ) = DueMonth
)
)
Measure "Outside time":
Outside time =
VAR DueMonth = MONTH ( FIRSTDATE ( Table1[due_date] ) )
RETURN
CALCULATE (
[Due],
FILTER (
Table1,
MONTH ( Table1[completed] ) <> DueMonth &&
NOT ISBLANK ( Table1[completed] )
)
)
Measure "Incomplete":
Incomplete =
VAR DueMonth = MONTH ( FIRSTDATE ( Table1[due_date] ) )
RETURN
CALCULATE (
[Due],
FILTER (
Table1,
ISBLANK ( Table1[completed] )
)
)
Measure "% Perf"
% Perf =
DIVIDE (
[Done on time],
[Due],
BLANK()
)
Group by due_date.month in report.
See https://pwrbi.com/so_55513978/ for worked example PBIX file.
EDIT after updated question in comment:
There are many ways you could approach creating a visualisation which combines these measures, with the monthly counts by completion date, if you really must. Here's one approach:
Create a "Report Columns" table:
Report Columns =
VAR ListMeasures =
DATATABLE (
"Header", STRING,
"Sort Order", INTEGER,
"First Of Month", DATETIME,
{
{"Due in month", 1, BLANK() },
{"Done on time", 2, BLANK() },
{"Outside time", 3, BLANK() },
{"Incomplete", 4, BLANK() },
{"% Perf", 5, BLANK() }
}
)
VAR ListMonths =
CALCULATETABLE(
GROUPBY (
ADDCOLUMNS (
DISTINCT ( Table1[completed] ),
"Header", FORMAT ( Table1[completed], "YYYY-MMM" ),
"Sort Order", YEAR ( Table1[completed] ) * 100 + MONTH ( Table1[completed] ),
"First Of Month", DATE ( YEAR ( Table1[completed] ), MONTH ( Table1[completed] ), 1 )
),
[Header], [Sort Order], [First Of Month]
),
Table1[completed] <> BLANK()
)
RETURN
UNION (
ListMeasures,
ListMonths
)
Set column Header to Sort By column Sort Order
Create Measure "Report Measure":
Report Measure =
IF (
NOT HASONEFILTER ( 'Report Columns'[Header] ),
BLANK(),
SWITCH (
VALUES ( 'Report Columns'[Header] ),
"Due in month", [Due],
"Done on time", [Done on time],
"Outside time", [Outside time],
"Incomplete", [Incomplete],
"% Perf", [% Perf],
CALCULATE (
[Due],
FILTER (
Table1,
DATE ( YEAR ( Table1[completed] ), MONTH ( Table1[completed] ), 1 ) = VALUES ( 'Report Columns'[First Of Month] )
)
)
)
)
Add a matrix visualisation, with Table1[due_date] in rows, Report Columns[Header] in Columns, and [Report Measure] in values. Format to suit.
Updated example PBIX file: https://pwrbi.com/so_55513978-2/

Related

Want to show null values in Dax

I have this sentence in DAX:
DEFINE
MEASURE 'BUYING SHOP'[FromDate] =
CALCULATETABLE (
DATEADD ( 'BUYING SHOP'[FROM_DATE], -1, YEAR ),
KEEPFILTERS ( TREATAS ( { #sadCode }, 'BUYING SHOP'[SAD_CODE] ) )
)
MEASURE 'BUYING SHOP'[ToDate] =
CALCULATE (
[Prior Completed Month],
KEEPFILTERS ( TREATAS ( { #sadCode }, 'BUYING SHOP'[SAD_CODE] ) )
)
MEASURE 'PRODUCT'[Maintenance_Repair] =
CALCULATE (
SUMX (
FILTER (
PRODUCT,
PRODUCT[PRODUCT_CATEGORY_CODE] = "MAINTENANCE_AND_REPAIR"
),
PURCHASE[Sum Purchases Prior]
)
)
EVALUATE
SUMMARIZECOLUMNS (
'DATE'[cMonth],
KEEPFILTERS ( TREATAS ( { #sadCode }, 'BUYING SHOP'[SAD_CODE] ) ),
KEEPFILTERS (
FILTER (
ALL ( 'DATE'[FullDate] ),
'DATE'[FullDate] >= 'BUYING SHOP'[FromDate]
&& 'DATE'[FullDate] <= 'BUYING SHOP'[ToDate]
)
),
"Maintenance_Repair", [Maintenance_Repair]
)
ORDER BY 'DATE'[cMonth] ASC
that returns this table:
enter image description here
What I want is that all months appear in the table even if it does not have any record.
for example:
03-Mar 20.8
04-Apr 0
05-May 222.04
06'Jub 0
and goes like that
Could anybody help me?
Try changing your measure to add zero.
MEASURE 'PRODUCT'[Maintenance_Repair] =
CALCULATE (
SUMX (
FILTER (
PRODUCT,
PRODUCT[PRODUCT_CATEGORY_CODE] = "MAINTENANCE_AND_REPAIR"
),
PURCHASE[Sum Purchases Prior]
)
) + 0

Power BI - Summarize table by 2 different date column

I have a table with information about our projects. I want to create a graphic showing number of projects started and finished each month. The original table looks like this:
Project ID
Start Date
Finish Date
1
02/10/2021
08/19/2021
2
02/15/2021
06/22/2021
3
05/08/2021
08/12/2021
4
06/25/2021
08/16/2021
5
06/29/2021
11/30/2021
6
08/12/2021
12/05/2021
The result table should look like this:
Month
Started
Finished
02/2021
2
0
05/2021
1
0
06/2021
2
1
08/2021
1
3
11/2021
0
1
12/2021
0
1
I've tried to use SUMMARIZE, but I haven't found a way to "group" 2 different dates in only 1 column.
Is there a way to do it?
First, create a Calendar Table.
Calendar Table
Calendar =
ADDCOLUMNS (
CALENDAR ( MIN ( 'Table'[Start Date] ), MAX ( 'Table'[Finish Date] ) ),
"Month Year", FORMAT ( [Date], "mm/yyyy" )
)
From there, you can create your DAX calculations
Started
Started =
VAR _SelectedMonth =
SELECTEDVALUE ( 'Calendar'[Month Year] )
VAR _FilterCalendar =
SELECTCOLUMNS (
FILTER ( 'Calendar', [Month Year] = _SelectedMonth ),
"#Date", [Date]
)
VAR _Result =
CALCULATE ( COUNTROWS ( 'Table' ), 'Table'[Start Date] IN _FilterCalendar )
RETURN
_Result
Finished
Finished =
VAR _SelectedMonth =
SELECTEDVALUE ( 'Calendar'[Month Year] )
VAR _FilterCalendar =
SELECTCOLUMNS (
FILTER ( 'Calendar', [Month Year] = _SelectedMonth ),
"#Date", [Date]
)
VAR _Result =
CALCULATE ( COUNTROWS ( 'Table' ), 'Table'[Finish Date] IN _FilterCalendar )
RETURN
_Result
Output

Count unique occurrences within a year

My database schema looks like down below
ID
Date
Status
ID1
2022/01/01
Active
ID1
2022/02/01
Active
ID1
2022/03/01
Active
ID1
2022/04/01
Terminated
ID2
2022/01/01
Active
ID2
2022/02/01
Terminated
I'd like to calculate unique occurrences from start of selected date year, till the selected date. My formula is:
CountOfUnique = CALCULATE( DISTINCTCOUNT( 'Table'[ID] ) , 'Table'[STATUS] = "Active", DATESBETWEEN('CALENDAR'[DATE], STARTOFYEAR('CALENDAR'[DATE]), MAX('CALENDAR'[DATE]) ))
In SQL I'd need something like
SELECT COUNT ( DISTINCT ID) FROM Table
WHERE STATUS = "ACTIVE"
AND DATE BETWEEN 2022/01/01 AND 2022/04/01
Try this:
CountOfUnique =
CALCULATE (
DISTINCTCOUNT ( 'Table'[ID] ),
'Table'[STATUS] = "Active",
DATESBETWEEN (
'CALENDAR'[DATE],
STARTOFYEAR ( 'CALENDAR'[DATE] ),
SELECTEDVALUE ( 'CALENDAR'[DATE] )
)
)
when you have a slicer on the visual, the start of selected date year doesnt mean much as you select the dates on the slicer. I created a Calendar Table = CALENDARAUTO() so it started from the 2022/01/01...
use one of these as you like...
sample PBix File
Unique Count =
VAR _max =
MAX ( 'Calendar Table'[Date] )
VAR _min =
MIN ( 'Calendar Table'[Date] )
RETURN
CALCULATE (
DISTINCTCOUNT ( 'Table'[ID ] ),
'Table'[Date ] <= _max
&& 'Table'[Date ] >= _min
)
or only Active if you need
Unique Count (Active) =
VAR _max =
MAX ( 'Calendar Table'[Date] )
VAR _min =
MIN ( 'Calendar Table'[Date] )
RETURN
CALCULATE (
DISTINCTCOUNT ( 'Table'[ID ] ),
'Table'[Date ] <= _max
&& 'Table'[Date ] >= _min
&& 'Table'[Status] = "Active"
)

Convert SQL query into DAX query for PowerBI visual

I have a table like below and trying to count the number of job_ids that are in "PAUSED" status in databricks and indicate it in PowerBI report.
If there is job id that has both of "PAUSED" and "UNPAUSED" status with the same timestamp, I should count that job_id as well as it's in the "PAUSED" status.
table1
job_id
status
timestamp
A
PAUSED
2022-08-02T21:09:13
A
UNPAUSED
2022-08-02T21:09:13
B
PAUSED
2022-08-10T21:09:15
B
PAUSED
2022-07-20T21:09:13
A
PAUSED
2022-08-12T21:09:13
C
PAUSED
2022-08-01T21:07:19
C
PAUSED
2022-08-01T21:07:19
C
UNPAUSED
2022-08-03T21:07:19
B
UNPAUSED
2022-07-20T21:09:13
A
UNPAUSED
2022-08-04T21:09:13
and this is the sql query I wrote
SELECT count(job_id) FROM (SELECT distinct job_id,status,DENSE_RANK over() partition
by job_id order by timestamp desc AS rn FROM table1) t2 WHERE
t2.rn = 1 and t2.status = "PAUSED"
What I want to do next is to display the number in PowerBI using the card visual.
To do that I think I need to convert the sql query above into DAX query to get the correct number..(not sure if there is better way to perform it, I'm new to PowerBI)
Anyone could help me with this query..?
Would appreciate any kind of help!
In Power BI you would need the following measure:
Paused Jobs =
CALCULATE(
DISTINCTCOUNT('Table'[job_id]),
'Table'[status] = "PAUSED"
)
You can then put this measure onto a Card visual next to your data table
I hope This is what you want :
If we test the summary table:
Paused_Jobs =
VAR SummaryTable =
ADDCOLUMNS (
CALCULATETABLE (
SUMMARIZE ( table1, table1[job_id], table1[status] ),
table1[status] = "PAUSED"
),
"Latest", CALCULATE ( VALUES ( table1[timestamp] ), LASTDATE ( table1[timestamp] ) ),
"TotalCount", CALCULATE ( COUNTROWS ( table1 ), LASTDATE ( table1[timestamp] ) )
)
VAR Result =
SUMX ( SummaryTable, [TotalCount] )
RETURN
SummaryTable
And the resulting screenshot:
You can check the result with sql query :
If It is what you want, and want it as a measure:
Paused_Jobs =
VAR SummaryTable =
ADDCOLUMNS (
CALCULATETABLE (
SUMMARIZE ( table1, table1[job_id], table1[status] ),
table1[status] = "PAUSED"
),
"Latest", CALCULATE ( VALUES ( table1[timestamp] ), LASTDATE ( table1[timestamp] ) ),
"TotalCount", CALCULATE ( COUNTROWS ( table1 ), LASTDATE ( table1[timestamp] ) )
)
VAR Result =
SUMX ( SummaryTable, [TotalCount] )
RETURN
Result

DAX Creating Calendar Table With Additional Field

I'm creating a calendar table in DAX:
Dates =
CALENDAR (
MIN ( 'Work Weeks'[Start Date].[Date] ),
MAX ( 'Work Weeks'[Start Date].[Date] )
)
The work weeks table contains a week number and a start date for each week.
For each date in my new Dates table, I want to assign a work week number using the start date of the work week. Note that work weeks start on different days of the week (they're assigned properly in my work weeks table though).
So what I'm trying is:
Dates =
ADDCOLUMNS (
CALENDAR (
MIN ( 'Work Weeks'[Start Date].[Date] ),
MAX ( 'Work Weeks'[Start Date].[Date] )
),
"Work Week",
CALCULATE (
MAX ( 'Work Weeks'[Start Date].[Date] ),
'Work Weeks'[Start Date] <= Date
)
)
I'm not sure how to reference the current row/date in the condition at the end. And then I also need to return the work week number, rather than just the start date.
Assuming your work week table looks something like this:
You can use this date table code to add the week number:
Date Table =
VAR ListOfDate =
VAR MinDate = MIN ( Sales[Order Date] ) -- Replace the reference with the column of your model
VAR MaxDate = MAX ( Sales[Order Date] ) -- Replace the reference with the column of your model
VAR StartDate = DATE ( 2021, 1, 1 ) -- DATE ( YEAR ( MinDate ), 1, 1 )
VAR EndDate = DATE ( 2021, 12, 31 ) -- DATE ( YEAR ( MaxDate ), 12, 31 )
VAR Result = CALENDAR ( StartDate, EndDate )
RETURN
Result
VAR WorkWeekTable = ALL ( WorkWeek )
VAR CalendarTable =
GENERATE (
ListOfDate,
VAR CurrentDate = [Date]
RETURN
ROW (
"Calendar Year Number", YEAR ( CurrentDate ),
"Calendar Month Number", MONTH ( CurrentDate ),
"Work week",
CALCULATE (
MIN ( WorkWeek[Work Week Number] ),
CurrentDate >= WorkWeek[Work Week Start Date],
CurrentDate <= WorkWeek[Work Week End Date],
REMOVEFILTERS ( )
)
)
)
RETURN
CalendarTable
The result will look like this: