PowerBI filter to change table data - powerbi

This is kind of an ambitious question for the current release of PowerBI.
I don't think this is possible, but what I would like to do is create a filter so that the user can select the first day of the week on the report, and then have that selection effect the tables in the table viewer (as opposed to only affecting the report). Does anyone know if this is possible? And if so, how can this be accomplished?
I'm using PowerBI tables as source data for Excel, so what I want is so that the user can specify "Monday" "Tuesday" etc. as the first day of the week and then that will somehow update the tables.

So you want to be able to use a filter to change the week day order in your table visualisations?
It is possible. Here's a proof of a concept:
Data:
MyTable(date, value)
01/01/2000, 5
02/01/2000, 8
...
StartOfTheWeek(index, name)
1, Monday
2, Tuesday
...
WeekDay(index, name)
1, Monday
2, Tuesday
...
Calculated column in MyTable:
WeekDay = WEEKDAY(MyTable[date])
Measure:
NewIndex =
mod(FIRSTNONBLANK('WeekDay'[index], 1) -
FIRSTNONBLANK(StartOfTheWeek[index], 1), 7) + 1
Relationship between MyTable.WeekDay and WeekDay.index
Add:
table visual with NewIndex, WeekDay.name and MyTable.value,
slicer on StartOfTheWeek.name
Click on NewIndex on the table visual to sort.
Result:
If you don't want to see newIndex column in your table, shrink its width to minimal. Obviously if you ever need to export table data it will have that column.
Not sure how best to order week days in StartOfTheWeek slicer, quick and dirty workaround is to change names to 1.Monday, 2.Tuesday...

I believe this is impossible, but there are a few posts suggesting this as an idea for future updates such as this one: https://ideas.powerbi.com/forums/265200-power-bi-ideas/suggestions/9414921-input-fields

Related

Power BI - Select Slicer Date Between 2 Columns

Hopefully a quick explanation of what I am hoping to accomplish followed by the approach we've been working on for over a year.
Desired Result
I have a table of SCD values with two columns, SCD_Valid_From and SCD_Valid_To. Is there a way to join a date table in my model (or simply use a slicer without a join) in order to be able to choose a specific date that is in between the two SCD columns and have that row of data returned?
Original Table
ID | SCD_Valid_From | SCR_Valid_To | Cost
1 2020-08-01 2020-08-03 5.00
Slicer date chosen is 2020-08-02. I would like this ID=1 record to be returned.
What We've Attempted So Far
We had a consultant come in and help us get Power BI launched last year. His solution was to create an expansion table that would contain a row for every ID/Date combination.
Expanded Original Table
ID | SCD_Valid_Date | Cost
1 2020-08-01 5.00
1 2020-08-02 5.00
1 2020-08-03 5.00
This was happening originally on the Power BI side, and we would use incremental refresh to control how much of this table was getting pushed each day. Long story short, this was extremely inefficient and made the refresh too slow to be effective - for 5 years' worth of data, we would need over 2000 rows per ID just to be able to select a dimensional record.
Is there a way to use a slicer where Power BI can select the records where that selected date falls between dates in two columns of a table?
Let me explain a workaround and I hope this will help you to solve your issue. Let me guess you have below 2 tables-
"Dates" table with column "Date" from where you are generating the date slicer.
"your_main_table" with with column "scd_valid_from" and "scd_valid_to".
Step-1: If you do not have relation between table "Dates" and "your_main_table", this is fine as other wise you have to create a new table like "Dates2". For this work around, you can not have relation between those tables.
In case you have already relation established between those tables, create a new custom table with this below code-
Dates2 =
SELECTCOLUMNS(
Dates,
"Date", Dates[Date]
)
From here, I will consider "Dates2" as source of your Date slicer. But if you have "Date" table with no relation with table "your_main_table", just consider "Dates" in place of "Dates2" in below measures creation. Now, Create these following 4 measures in your table "your_main_table"
1.
date_from_current_row = max(join_using_date_range[SCD_Valid_From])
2.
date_to_current_row = max(join_using_date_range[SCD_Valid_to])
3.
date_selected_in_slicer = SELECTEDVALUE(Dates2[Date])
4.
show_hide_row =
if(
[date_selected_in_slicer] >= [date_from_current_row]
&& [date_selected_in_slicer] <= [date_to_current_row]
,
1,
0
)
Now you have all instruments ready for play. Create your visual using columns from the table "your_main_table"
Final Step: Now just add a visual level filter with the measure "show_hide_row" and set value will show only when "show_hide_row = 1".
The final output will be something like below image-

PowerBI - conditional formatting for highlighting weekends

I have a data table matrix in power bi showing Dates in Rows and hours in columns and count of customers against them as values. i wanted to highlight all rows of thursday, friday and saturday, so that weekend figures can be compared.
Need help in this pls...
the table is in the below format
enter image description here
First you want to create a duplicate (in my case monday - friday (2) that does NOT have a relation to you source table. You want this duplicate table to only show days. This duplicate table needs a relationship with the slicer you are going to use to filter for days
In your source table (monday - friday) you want to create the following measure:
Measure = IF(HASONEVALUE('monday -friday (2)'[Day]);
IF(SELECTEDVALUE('monday -friday (2)'[Day]) = MAX('monday -friday'[Day]); 1; 0);
IF(ISFILTERED('monday -friday (2)'[Day]) && COUNTROWS(FILTER('monday -friday (2)'; 'monday -friday (2)'[Day] = MAX('monday -friday'[Day]))); 1; 0))
Now you want to create conditional formatting. First create your table with days and values (for the example i did not sort the table. You want to sort your table so it looks better):
Then right click on values and select conditional formatting. Select based on rules.
and the measure you just created in your source table.
for the first option you select "is bigger than or equal to" and give it a value 1.
for the second option you select "is smaller than"and give it a value 1000.
Choose the color you want your highlights to be.
If you now select a day in your filter the value for that day will show the background color you selected! In your case you want to select all days you want to have highlighted.
Hope this helps!

Need to limit the date in a slicer by today's date in Power BI

I am building a report in Power BI Desktop, created a slicer - YearMonthSort - which has data selection by Year-Month
Plz, see the screenshot below:
My goal is to limit data in this slicer as -
2015-07 to today's date
(whichever it will be when users will look at the data,
in the same format - "YYYY-MM")
In the "Filters" section I can select my starting year as 2015-07,
but having problem setting today's date.
I tried to create the new Measure, but not sure where to place it,
or, may be there is another way to perform this:
IsToday = FORMAT(TODAY(), "mm/yyyy")
I only just started to learn this,
Thank you for help in advance!
You can do the following:
I am assuming that you are using a calendar table that is joined to your fact table on a date field and that your year sort column is calculated with reference to the 'date' field in your calendar table.
Assuming, this is true you can create a calculated column that will flag whether or not the date field is before or after today. This can be achieved by using the following DAX:
IsToday =
SWITCH (
TRUE (),
'Calendar'[Date]
<= NOW (), 1,
0
)
Then, in your visual add a 'Visual Level Filter' using this new column and set it to 1.

Measure in DAX to calculate YTD for chosen month only for Power BI

How to construct DAX measure to calculate sum of YTD value for specific month?
Here we have FactTable grouped by months. FactTable is filled with both Actual data and Forecast data. The only way to know when Actual end is information in table [Cut of date] in column [End of YTD]. In table [Cut of date] in column [End of YTD] – it is a single value table – we have the interesting chosen month, for which we want to see the calculation of YTD. In our case it is March. FactTable is updated irregularly every month with usually one month delay. There is no way of linking it to time functions like TODAY because of irregular update.
We would like to have a correct value of YTD displayed in yellow Card Visual for the month [End of YTD]. When we click on the slicer on "2018-03" we get almost what we want – correct value of 66 in the yellow Card. However this solution is not automatic. I want to see correct value automatically when the [End of YTD] month changes, in our case to April or then to May. I do not want it done by user.
My desperate effort can be downloaded from file: DAX YTD.pbix
I pursued the deer in various ways:
By using FILTER function in DAX measures. But it seems that the
FILTER function is to harsh. It is applied to fact table first,
selecting only one month, and then calculating YTD value wrongly. So if
there would be any option for forcing order of calculation and filtering, there would
be hope.
I tried SWITCH function to display proper result
for specific month and 0 or null for other months. Although I
succeed in this, I was not able to take advantage of it. When it
came to filtering I was as hopeless as before. BTW I would be able
to make it if SWITCH produced totals at the end of the table, but it
does not. Surprisingly.
I put some hopes in RELATED function to display proper results in the [Cut off date] table. I have not walk out of the fog so far.
I would appreciate your help.
Update before bounty.
Going to higher level. I have introduced a Category column to FactTable. Please download DAX YTD by category.pbix. So filtering gets more complex now. I would like to have correct YTD figures for Apples category.
Did you use the Date column from the Calendar table, instead of the one from FactTable?
If you use the date column from FactTable, when you apply a filter on the date, it will filter on the fact records which is in March, and then do the calculation afterwards, hence the result 33.
If you use the one from Calendar, when you apply a filter on it, it filters the records on Calendar (which you want to show in the chart), so the underlying calculation will still remain intact.
A working example:
Calendar = CALENDAR(DATE(2010, 1, 1), DATE(2020, 12, 31))
I suggest you to change the calculations of the measures to avoid missing values in some cases:
Total = SUM(FactTable[Value])
MTD = TOTALMTD([Total], 'Calendar'[Date])
YTD = TOTALYTD([Total], 'Calendar'[Date])
UPDATE:
It's much clearer to me what you want to achieve now but it still seems an XY problem to me.
I understand that you want to show the dashboard as is so that users do not need to click/input every time to see what they are supposed to see. That's why I don't get why you need to create a new table to store the Cut off date (End of YTD). How is it going to be maintained automatically?
The relative date filtering solution above actually still works in the .pbix file you've shared. If you drag the Date column from the Calendar table to visual level filters for the yellow card and add the relative date filtering, it should work as below:
For the End of YTD visual, you can use the following measure to get the first day of last calendar month, so you don't need to create another table for it:
End of YTD = EOMONTH(TODAY(), -2) + 1
And hopefully this is what you want to achieve:
Updated file for your reference.
UPDATE again:
I think you'll have to write your own YTD calculation instead of using the built-in one, so that you can make use of the cut off date you defined in another table. Here I assume that you have one and only one row in 'Cut off date'[End of YTD]. Note that I've added ALL() to the filter, so that the yellow card remains the same (66) instead of showing blank when some other rows/filters are clicked:
YTD_Special =
CALCULATE(
[Total],
FILTER(
ALL(FactTable),
FactTable[Date] >= DATE(YEAR(VALUES('Cut off date'[End of YTD])), 1, 1) &&
FactTable[Date] <= VALUES('Cut off date'[End of YTD])
)
)
I would resolve this by adding a calculated column to your Calendar table to categorise each row into either "YTD" or "Other", e.g.
Is YTD =
IF (
[Date] >= DATE ( YEAR ( DISTINCT ( 'Cut off date'[End of YTD] ) ), 1, 1 )
&& [Date] <= DISTINCT ( 'Cut off date'[End of YTD] ),
"YTD",
"Other"
)
I would then add the new Is YTD field to the Visual level filters of your Card visual, and choose YTD from the Basic filtering list. The measure shown can be your simple Total measure: SUM(FactTable[Value]).
This is a far more flexible and resuable solution than any specific measure gymnastics. You will not need an explosion of measures to apply the required logic on top of every base measure - they will all just work naturally. You can apply the filter at any level: Visual, Page, Report, or put it in a Slicer for control by the end user.
I prefer to return text results e.g. "YTD" / "Other" (rather than 1/0, True/False or Yes/No), as this allows for easy extension to other requirements e.g. "Prior YTD" (1 Jan 2017 to 1 Mar 2017). It also is clearer when used in visuals.
Actually I shouldn't claim the credit for this design - this roughly follows how Cognos Transformer's Relative Time functionality worked back in the 90s.
I did something like this in my Periodic/YTD report (last sheet): http://ciprianbusila.ro/
I have used the index value of the month selected (range 1-12) and based on this I have created a measure using max function please see the code below:
ACT = var
ACT_periodic=calculate([Value],Scenarios[Scenario]=values(Scenarios[Scenario]))
var max_month=max(Periods[Period Order])
var ACT_YTD=CALCULATE([Value],Scenarios[Scenario]=VALUES(Scenarios[Scenario]),all(Periods[Month]),Periods[Period Order]<=max_month)
var myselection=if(HASONEVALUE(MRD_view[.]),values(MRD_view[.]),"PERIODIC")
return
switch(
true(),
myselection="PERIODIC",ACT_periodic,
myselection="YEAR TO DATE",ACT_YTD,
ACT_periodic
)

PowerBI - Time Lag Calculation in Dates, using Query Editor

I'm new to Power BI Desktop, coming from Excel.
In the query editor, I would like to create a new column in a table with the difference in time/date from one record to the next, by a separate grouping column (device). An example explains it better. Here's the starting point for the data, with one column for the device id, and another for the Date of the event.
Device Date
A 5/1/2016
B 5/1/2016
C 5/2/2016
A 5/4/2016
B 5/5/2016
A 5/10/2016
B 5/9/2016
C 5/12/2016
I would like to group by Device and Sort by Date, then calculate the differences, to make something like this:
Device Date Lag
A 5/1/2016 (null)
A 5/4/2016 3
A 5/10/2016 6
B 5/1/2016 (null)
B 5/5/2016 4
B 5/9/2016 4
C 5/2/2016 (null)
C 5/12/2016 10
What's the best way to do this in Power BI query editor?
Thanks for the help!
Here's a solution. But it does not involve Query editing.
we're gonna create a calculated column and do it the dax way.
1) In your Fields pane, right click on the table and Select "New
Column"
2) In the formula bar, type in the formula below. replace TableName with your table name
LagColumn = DATEDIFF(TableName[Date]
, CALCULATE(MAX(TableName[Date]),
FILTER(TableName,
TableName[Device] = EARLIER(TableName[Device]) &&
TableName[Date] < EARLIER(TableName[Date])
)
)
, DAY
)
3) Select Device, Date and the LagColumn in your report now. Choose "Table" option from the visualization panel.
Explanation of the formula -
1) The new lagColumn is the DATEDIFF in days of two entities.
2) First argument is the DATE field of the current row
3) Second argument is the maximum date value of all the dates that are less than the current row's date and that belongs to the same device as the current row. (EARLIER helps you retrieve the value of the current row in the previous context.)
read more here
1) EARLIER - https://msdn.microsoft.com/en-us/library/ee634551.aspx
2) Row Context and Filter Context - https://www.sqlbi.com/articles/row-context-and-filter-context-in-dax/
Screenshot of a similar use-case:
Here,
AccountAlternateKey corresponds to Date
ParentAccountAlternateKey
corresponds to Device, and
LagColumn is LagColumn
Note - For every group, lag of the first column is the first column itself. If you want it to be NULL, you can check if ISBLANK(CALCULATE....) is true and then make it null. that just adds a little bit complexity to the formula..
Proposing DAX solution since i don't think there is a query way to deal with this, AFAIK.
Let me know if you have any more questions..