DAX Creating Calendar Table With Additional Field - powerbi

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:

Related

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"
)

How to Create a Slicer that uses dates and that only contains YTD Months dynamically by current date in PowerBI

Trying to create a slicer that when clicked only the months YTD, specifically up the the last month of current.
Such as slicer box clicked: (Say current month is August) then Months "Jan","Feb",..."July" would only display to click.
I've tried several things, my last attempt was to create a Dax table with dates and try to do a switch:
Dates =
var CurrentMonthInt = month(TODAY())
var monthis8 = "Jan" + "Feb" + "March" + "April" + "May" + "June" + "July"
VAR BaseTable =
CALENDAR(
DATE( YEAR ( MIN(Reporting[InvoiceDate])),01,01),
DATE( YEAR ( MAX(Reporting[InvoiceDate])),12,31)
)
RETURN
ADDCOLUMNS(
BaseTable,
"Year", YEAR([Date]),
"Month", FORMAT([Date],"mm"),
"Year Month", FORMAT([Date],"YYYY MM"),
"Month YTD", SWITCH(TRUE(),
CurrentMonthInt = 1, "Jan",
CurrentMonthInt = 8, monthis8,
"testing"
)
)
this returns a variant data type and will not work. I am thinking this is trying to add them all up on the same row.
Hello Please test this and let me know if It solves your problem.
Dates =
VAR CurrentMonthInt =
MONTH ( TODAY () )
VAR BaseTable =
CALENDAR (
DATE ( YEAR ( MIN ( Reporting[InvoiceDate] ) ), 01, 01 ),
DATE ( YEAR ( MAX ( Reporting[InvoiceDate] ) ), 12, 31 )
)
RETURN
FILTER (
ADDCOLUMNS (
BaseTable,
"Year", YEAR ( [Date] ),
"Month", MONTH ( [Date] ),
"Year Month", FORMAT ( [Date], "YYYY MM" )
),
[Month] <= CurrentMonthInt - 1
)
Note: I haven't tested it.

Switching Measure in a line chart with the Slicer selection on Power BI

I have a line chart that shows YTD revenue for the last 3 years and a line for Goal.
I have 3 slicers Region, District, and Branch all coming from the table DIMBRANCH
I have the following goals tables:
goalbyRegion
goalbyBranch
goalbyCompany
Requirement When nothing on the slicer is selected then the goal from table goalbycompany should be displayed.
when a region is selected, show goal on the line chart of the selected region coming in from goalbyregion table.
Problem Currently the revenue changes over slicer selection because the revenue is coming from a single table tbl_revenue.
The image shows the revenue and goal line charts if nothing is selected on the slicer.
DAX currently used:
2017/Revenue =
VAR _year =
YEAR ( TODAY () ) - 2
RETURN
CALCULATE (
SUM ( Revenue[Revenue] ),
FILTER ( Revenue,Revenue[Year] = _year )
)
2018/Revenue =
VAR _year =
YEAR ( TODAY () ) - 1
RETURN
CALCULATE (
SUM ( Revenue[Revenue] ),
FILTER ( Revenue,Revenue[Year] = _year )
)
2019/Revenue =
VAR _year =
YEAR ( TODAY () )
RETURN
CALCULATE (
SUM ( Revenue[Revenue] ),
FILTER ( Revenue,Revenue[Year] = _year )
)
2019/Goals Amount =
VAR _year =
YEAR ( TODAY () )
RETURN
CALCULATE (
SUM ( GoalByCompany[GoalAmount] ),
FILTER ( GoalByCompany, GoalByCompany[Year] = _year )
)
DAX I am trying to use to achive my goal with switch function.
Switch Goal =
var _region = SELECTEDVALUE(Branch[Region])
var _branch = SELECTEDVALUE(Branch[Branch])
var _district = SELECTEDVALUE(Branch[District])
var _year = year(TODAY())
var _goalregion = CALCULATE(
SUM(GoalByRegion[GoalAmount]),
FILTER(GoalByRegion , GoalByRegion[year] = _year))
var _regionselection =
SWITCH(_region,
"RegionGoal",_goalregion
)
return
switch (_regionselection,"Region",_goalregion,blank())
I am not used to the switch function yet. I thought I could use this to get to my requirement.
this is when I select a region.
Here is the relationships between my tables
Additonal tables: revenue table links to branch table with branchID
calendarweek table links to date table via weekendingdate column.
You don't need to use SWITCH, if you're only considering whether Region is filtered or not. You can simply use an IF statement, to decide which measure to calculate:
2019/Goals Amount =
VAR _year =
YEAR ( TODAY() )
RETURN
IF (
HASONEFILTER ( Branch[Region] ),
CALCULATE (
SUM ( GoalByRegion[GoalAmount] ),
FILTER ( GoalByRegion, GoalByRegion[Year] = _year )
),
CALCULATE (
SUM ( GoalByCompany[GoalAmount] ),
FILTER ( GoalByCompany, GoalByCompany[Year] = _year )
)
)

Power BI - Matrix count of values in query

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/