Find Maximum month from two slicers - powerbi

I have this data in a pbix file:
ImaginaryData =
DATATABLE (
"Month", DATETIME,
"Amount", INTEGER,
{
{ "01 JAN 2018", 10 },
{ "01 FEB 2018", 15 },
{ "01 MAR 2018", 25 },
{ "01 APR 2018", 60 },
{ "01 MAY 2018", 55 }
}
On the canvas we have two Date field slicers:
How do I create two measures?:
Max Month Selected
Min Month Selected
I tried this for Max Month Selected but it does not work:
Max Month Selected =
Calculate(
MAX(ImaginaryData[Month]),
ImaginaryData
)

When two or more slicers filter the same column, you will always get the intersection of values - not the union. So in other words, if you select one value in the first slicer, and a different value in the second slicer, you would basically end up with a blank filter. That can also be seen if you drag the [Amount] column into your report:
Instead, you should use a single slicer and allowing the user to select a range of months, for example using the "Between" option:
Then, the following measures should work as expected:
Max Month Selected = MAX(ImaginaryData[Month])
Min Month Selected = MIN(ImaginaryData[Month])
If you must have two dropdown slicers, the only workaround is to create two tables, both containing the [Month] column, and have one slicer pointing to one table, and the other slicer pointing to the second table. Then, you would define your measures as:
Max Month Selected = MAX(MAX('MonthTable1'[Month]), MAX('MonthTable2'[Month]))
Min Month Selected = MIN(MIN('MonthTable1'[Month]), MIN('MonthTable2'[Month]))

Related

How to show the records which are part between selected date range in Dax

I have a below table in Power BI. This table will have the details of campaigns which are ran on specific period.
Campaign Name StartDate Enddate
AAA 01-05-2022 30-04-2022
BBB 01-04-2022 30-04-2022
CCC 01-04-2022 30-04-2022
DDD 01-04-2022 30-09-2022
EEE 01-03-2022 30-09-2022
FFF 01-03-2022 30-09-2022
Now i am using the start date in the slicer. so if i select date range of Apr-22 to Jun-22, table should display whatever campaigns which are active between the selected period. In this case, it should display all the values from the above table, because all the campaigns are between Apr-Jun. but in my case, last two rows are not displaying since the start date is in March but these campaigns are also active during the period of Apr-22 to Jun-22. is there a way we can create some measure to show the campaigns, even if start date before selected slicer date range but end date falls between the selected date range?
You can try smth like this. The measure should return 1 for “active” if a company name is in scope and total number of active in total. But it would be better to create a DateTable as Ozan Sen advise. Otherwise you can get a wrong output - a slicer will return max and min dates that you have in table not as slicer shows. For instance your slicer shows you the first date as 01 Jan, but in your facts the minimum is 3 Jan, so, this you will get as minDate. The problem can come if the endDate is 02 Jan, the measure will not count it, because the minDate=03 Jan. With a Date table you'll avoid this kind of problem. I didn't check the measure, so I'm not 100% sure it works.
VAR minDate =Min('table'[startDate])
VAR maxDate =Max('table'[startDate])
VAR noOfCompNames =
CALCULATE(
CountRows(table)
,OR(
AND('table'[startDate]>=minDate,'table'[startDate]<= maxDate)
,AND('table'[endDate]<=minDate,'table'[endDate]>=maxDate
)
)
RETURN
noOfCompNames

Slicer on custom date-quarter range

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]):

PowerBi DAX measure to sum duration of timespans filtered by current slicer

I need a DAX measure that gives me the sum of durations for multiple categories restricted by a date slicer.
In this simplified example there are 2 categories with 3 subcategories each. A DateTime Slicer on the dashboard is set to the timespan of 2nd of January 2021 noon to 6th of January midnight. I need the summed up duration of all categories in this timespan.
Input data:
A table containing multiple rows for each category with a start date and an end date.
The complicated part is that there are pauses between the timestamps.
Desired output:
A table on the dashboard containing the category and a calculated measure for the summed up duration during the sliced timespan.
When changing the slicer the meaure shall change as well.
My current solution for this problem is an M formulato create a list of all days in each timespan and to unpivot all lists. In the dashboard the count of rows gives you the number of days in the selected timespan. This solution though reqires a much larger input table and soes not work if you want to be exact on the second, only on days.
I tried so solve this via a measure but didn't make any progress worth showing here.
all datetime values are in the format dd.mm.yyyy hh:mm:ss (24h system)
I found a way to do it by using 2 measures.
First measure calculates the time during the timespan for each element:
I use one Date Table only consisting of all dates available which is the input for the slicer and the data Table called "Data".
duration_in_timespan_single =
VAR MinTs = MIN ('Date'[Date])
VAR MaxTs = MAX ('Date'[Date])
VAR MinUtcMin = MIN ('Data'[Date_Start])
VAR MaxUtcMax = MAX ('Data'[Date_End])
RETURN
IF(
AND(MinUtcMin >= MinTs, MinUtcMin <= MaxTs),
IF(
MaxUtcMax <= MaxTs,
CONVERT((MaxUtcMax-MinUtcMin),DOUBLE),
CONVERT((MaxTs-MinUtcMin),DOUBLE)),
IF(
MinUtcMin < MinTs,
IF(
MaxUtcMax > MinTs,
IF(
MaxUtcMax <= MaxTs,
CONVERT((MaxUtcMax-MinTs),DOUBLE),
CONVERT((MaxTs-MinTs),DOUBLE)
),
0
),
0
)
)
The second measure just sums up the first for each category:
duration_in_timespan = SUMX('Data',[duration_in_timespan_single])

Calculate date diff between column date for each row and min slicer date

I think that the following formula summarize pretty well what I want to achieve:
date diff =
ABS (
DATEDIFF (
data_table[login_date],
SELECTEDVALUE ( 'Date'[Date] ),
DAY
)
)
but it returns me the following error
A single value for column 'login_date' in table 'data_table' cannot be determined. This can happen when a measure formula refers to a column that contains many values without specifying an aggregation such as min, max, count, or sum to get a single result.
In other word I want have a column in my data_table with date diff calculated dynamically based on min slicer date selection.
My final goal is to filter out dynamically users that has not been logged for the last 3 months based on the slicer date range.
Here is the dataset
user_id, login_date
111, 01/02/2021
222, 02/15/2021
444, 03/15/2021
555, 01/15/2021
I want user ID to be filtered out when the number of days between the max date of the date range and the day of the last connection is higher than 90 days.
Edit
I'm adding a different formula I'm working on but having few issues to make it work
active users = CALCULATE( DISTINCTCOUNT(data_table[id]), ( FILTER ( VALUES ( data_table[id] ), DATEDIFF(IF( ISBLANK(SELECTEDVALUE(data_table[login_date])),[Min range date],SELECTEDVALUE(data_table[login_date])),[Max range date],DAY) < 90 ) ))
You can't have a dynamically calculated column, but you can use a measure to do this. The issue that you have with your calculation is that it needed to do a row by row evaluation, rather than over a column. That is why you get an 'A single value for column 'login_date' in table 'data_table' cannot be determined' error.
In this case you can use SUMX, as this is a iterator and it will do row by row. So using the following measures:
Selected Date = SELECTEDVALUE('Calendar'[Date])
This reads the date selected. You can wrap it with a MIN/MAX if needed depending on how your slicer is set up. You can change the slicer to single select, it you just want one value.
Date Calc = SUMX('Table', DATEDIFF('Table'[Login_date], [Selected Date], DAY))
This uses SUMX to calculate on a row by row level.
You can then use this measure to drive your visual. In this example I've filtered out those over 30 days since the login
If you choose a new date, it will recalculate straight away. This should set you on the right path for your use case.

Filter table based on a specific date plus 7 days

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.