In Power BI, my ideal solution is a between slicer with date values of the form yyyy-QQ where the default (THAT CAN BE MODIFIED) is the prior quarter and extends for the next year (e.g., 2022-Q1 to 2023-Q1), like so:
I have a date table with dates, including quarters, and am open to achievable modifications on this theme, such as a start quarter and number of quarters, date slicer that defaults to quarter boundaries, etc.
Requirements:
Must use quarters
Quarters/range (including number of quarters) must be modifiable by user
Cannot use list or dropdown (my date range is over 50 years)
Thanks for both ideas and code snippets!
The solution I ended up going with was to use a slicer named "Quarters From Today." In my date table, I added a column to compute this value:
Date = ADDCOLUMNS(
CALENDAR( DATE(2018, 01, 01), DATE(2032, 12, 31) ),
"Quarters from Today", DATEDIFF(TODAY(), [Date], QUARTER) )
I then added a slicer on the Quarters from Today field:
And to assist the user, I added the dates to the chart title using MIN('Date'[Date]) and MAX('Date'[Date]):
Related
I have 2 date slicers and a measurement which is [Profit], I would like to return the difference in Profit given the date selections. Please see below for detail.
Date Slicer 1: 3/7/2021-3/13/2021
Date Slicer 2: 3/14/2021-3/20/2021
Profit given Date Slicer 1: 10
Profit given Date Slicer 2: 15
Value I am trying to get = 5 (15-10)
If the two slicers come from the same Date column, both periods do not intersect each other and you get a filter without dates.
Possibly there are better solutions but one of them would be to use a Harvester Filter (Creating an additional table containing a copy of the date column unrelated to the model, aka Disconnected Tables)
Before moving on to the next part you can verify that the filter contains the same dates but since it is a disconnected table, it does not filter the model, yet
For the [Profit given Date Slicer 1] you can use the original measure
For the [Profit given Date Slicer 2] you must change the filter to:
DATESBETWEEN(
'Dates'[Date],
MIN(HarvestingDates[Date]),
MAX(HarvestingDates[Date])
)
Problem:
Calculate the total for period of prior year of the period(s) selected in slicer. Table has amount, period, and a date, ie 11/1/2020 for period 11/2020, based on the period.
Attempted:
Slicer periods selected are 10/2020 and 11/2020.
Table visual contains the period and amount columns. Data Model Table contains columns, account, amount, period, and period date. Multiple accounts per period will have the same period date, to sort the periods. Example, account 1 through 3 will all have 11/1/2020 as period date for period 11/2020. Calculated Measure created to calculate last year amount is as follows:
Total For Period Last Year =
CALCULATE(
Sum(‘Table’[Amount])
, Filter(
‘Table’
,SAMEPERIODLASTYEAR(‘Table’[Period Date])
)
)
Results:
Calculated measure is added to table but only shows the amount for the period selected in slicer and not the period for the same period but of the prior year.
So 10/2020 amount would be $2000 and the calculation should show amount for 10/2019 with ie $1500.
Period | Amount | Total For Period Last Year
11/2020 $2000 $2000
10/202. $1500 $1500
Desired Result:
Period | Amount | Total For Period Last Year
11/2020 $2000 $1500
10/2020 $1500 $1000
To use time intelligence functions a well formed date table should be used. It's possible to create one using various techniques; an easy one can be found here
DAX 101: Creating a simple date table in DAX
That said, the error in your code is that you iterate over the Table part in the current filter context. You might see a better result (but maybe still not correct, because a Date table is missing, using ALL( 'Table' ) instead of just 'Table'. This way you would FILTER over the whole table instead of just the portion in the current period.
Total For Period Last Year =
CALCULATE(
SUM( 'Table'[Amount] ),
FILTER( ALL( 'Table' ), SAMEPERIODLASTYEAR( 'Table'[Period Date] ) )
)
I'm not sure, since I never use auto date/time, but this could also work by using the Power BI generated Date table when auto date/time is enabled (and if I correctly remember the synstax on how to use it)
Total For Period Last Year =
CALCULATE(
SUM( 'Table'[Amount] ),
FILTER( ALL( 'Table' ), SAMEPERIODLASTYEAR( 'Table'[Period Date].[Date] ) )
)
To use Time intelligence like sameperiodlastyear, you must use date table (Mark as Date Table). Read this description:
https://dax.guide/sameperiodlastyear/#
In order to use any time intelligence calculation, you need a well-formed date table. The Date table must satisfy the following requirements:
All dates need to be present for the years required. The Date table must always start on January 1 and end on December 31, including all the days in this range. If the report only references fiscal years, then the date table must include all the dates from the first to the last day of a fiscal year. For example, if the fiscal year 2008 starts on July 1, 2007, then the Date table must include all the days from July 1, 2007 to June 30, 2008.
There needs to be a column with a DateTime or Date data type containing unique values. This column is usually called Date. Even though the Date column is often used to define relationships with other tables, this is not required. Still, the Date column must contain unique values and should be referenced by the Mark as Date Table feature. In case the column also contains a time part, no time should be used – for example, the time should always be 12:00 am.
The Date table must be marked as a date table in the model, in case the relationship between the Date table and any other table is not based on the Date.
Remarks
The dates argument can be any of the following:
A reference to a date/time column. Only in this case a context transition applies because the column reference is replaced by
CALCULATETABLE ( DISTINCT ( ) )
A table expression that returns a single column of date/time values.
A Boolean expression that defines a single-column table of date/time values.
The result table includes only dates that exist in the dates column.
Internally SAMEPERIODLASTYEAR corresponds to the following call of DATEADD:
DATEADD ( <Dates>, -1, YEAR )
I'm having some difficulty getting a YoY change % for values in Power BI. The averages don't come out proper. I've come to understand this is an AVERAGE vs AVERAGEX issue in Power BI.
I need to create charts of year on year growth monthly. So Jan20 % change from Jan 19. I thought what was below was correct, but it is always throwing an issue for the month of February and a few other months. But some of the months are correct. My Measure is below.
Growth =
IF(
ISFILTERED('Oct5_5'[TRAFFIC_DTE]),
ERROR("Check Time Filter."),
VAR PrevMonth =
CALCULATE(
AVERAGE('Oct5_5'[VISITS_AMT]),
DATEADD('Oct5_5'[TRAFFIC_DTE].[Date], -12, MONTH)
)
RETURN
DIVIDE(
AVERAGE('Oct5_5'[VISITS_AMT]) - PrevMonth,
PrevMonth
)
)
Snippet
Can someone please show me how to use the right Average? Thank you so much!
Do You have all calendar days in your traffice_dte?
Probably not. This causes you to go back to a date that does not exist, which causes an error.
DATEADD function work ok if you use it on "calendar" tabel.
Requirement below:
https://dax.guide/dateadd/
The Date table must always start on January 1 and end on December 31, including all the days in this range. If the report only references fiscal years, then the date table must include all the dates from the first to the last day of a fiscal year. For example, if the fiscal year 2008 starts on July 1, 2007, then the Date table must include all the days from July 1, 2007 to June 30, 2008.
There needs to be a column with a DateTime or Date data type containing unique values. This column is usually called Date. Even though the Date column is often used to define relationships with other tables, this is not required. Still, the Date column must contain unique values and should be referenced by the Mark as Date Table feature. In case the column also contains a time part, no time should be used – for example, the time should always be 12:00 am.
The Date table must be marked as a date table in the model, in case the relationship between the Date table and any other table is not based on the Date.
I have a table containing a date field (from 1 March 2020 to now) that I need to filter to a specific date and the previous 6 days to give complete week's data. So if I chose 30 March I'd get a table of 24 March to 30 March. If I then chose 31 March the table would show 25 March to 31 March.
I can use a date slicer to choose a range of dates but I want to be able to pick a single date, with Power BI automatically selecting the earlier date.
Any pointers much appreciated.
Mark.
You can create two measure - one for Slicer selected date and Another one with 7 day minus from the selected date as below-
Considering your date table name is- Dates
selected_date = SELECTEDVALUE(Dates[Date])
seven_day_starts_from = DATEADD(Dates[Date],-7,DAY)
Now create your calculated measure first like-
total_sales = SUM(Sales[sale])
Here comes how you will always calculate last 7 days sales considering the selected date in the slicer-
7_day_sales =
(
CALCULATE(
[total_sales],
DATESBETWEEN(
'Dates'[Date],
[seven_day_starts_from],
[selected_date]
)
) + 0
)
Remember, this is just a sample flow showing how it should work. You should try to follow the steps with your data and table structure. Dates table is a calendar table and Sales table is connected to the Dates table using the Date column.
I am new to Power bi but i want to use Slicer(user selection) while filter data with comparison by 'Greater Than' instead of equal to.
I have data of devices with LastUpdated date column. and slicer with few list of dates for 15 days gap (calendar would be better but as it is not available yet sticking with dates list)
When user select a date in Slicer i want to filter the data whose Lastupdated Date is greater than equal to selected date. How to achieve this? tried columns,measures..
Any help is appreciated.
I think you can create a measure that links the Date list table and the Device table, even if there is no relationship between them. However the measure must be present in your visualization otherwise the slicer will not affect it.
I've created a measure that calculates the maximum date for those rows which last update date is greater or equal than the slicer selection.
CalculatedLastUpdate =
CALCULATE (
MAX ( DeviceTable[LastUpdate] ),
FILTER (
DeviceTable,
DeviceTable[LastUpdate] >= MINX ( DateList, DateList[Date] )
)
)
DateList - a table containing a column [Date] with you date range.
DeviceTable - a table containing device data.
Now you can delete LastUpdate column from your visualization in order to avoid two columns with the same data.
Let me know if it helps.
I don't know about earlier , but now you can modify the date slicer to do as "after" the given date (you can do so by clicking on the icons present in the rightmost side of the slicer visual itself , wher you can select mode of slicer as between , after ,list , dropdown etc.)...which I believe means that you get all data for dates greater than the selected dates for the given column used in slicer which in your case will be LastUpdate.