I have a query I tried to resolve but I failed badly.
I have a table with items and want to count active ones per day. I did it in excel where I created another column with the last 7 days and I used countifs.
So my excel formula is
=COUNTIFS($A$3:$A$30001,"<="&F3,$B$3:$B$30001,">="&F3)
But I need the same in Power BI.
My table with data is called ADW_DEFECTS and has two columns with open and closed dates.
ISSUE_DTTM CLOSE_DTTM
26/11/2019
26/11/2019
26/11/2019
26/11/2019
26/11/2019
25/11/2019
25/11/2019
25/11/2019
24/11/2019 25/11/2019
24/11/2019 25/11/2019
24/11/2019 25/11/2019
24/11/2019 26/11/2019
23/11/2019 24/11/2019
23/11/2019 24/11/2019
23/11/2019 25/11/2019
22/11/2019 22/11/2019
Now I figure out I need to pivot to another table for the last seven days' calculations (I used a table called NEW.DEFECTS_ACTIVE).
This is the result of excel
Dates Active
26/11/2019 1
25/11/2019 5
24/11/2019 7
23/11/2019 3
22/11/2019 1
21/11/2019 0
20/11/2019 0
So, the required result in Power BI is a table with the same as excel one.
You can also expand the table like this, though you end up with a second table:
Expanded_ADW =
GENERATE(
CALCULATETABLE(
ADW_DEFECTS;
ADW_DEFECTS[CLOSE_DTTM] <> BLANK()
);
SELECTCOLUMNS(
GENERATESERIES(
ADW_DEFECTS[ISSUE_DTTM];
ADW_DEFECTS[CLOSE_DTTM]
);
"Expanded_Date"; [Value]
)
)
Then have a calendar with a 1:* relationship with [Expanded_Date]. In a visual table with date from the Calendar tabel add this measure:
Active = COUNTROWS('Expanded_ADW')+0
Then you have a table like this:
First I would create a Dates table. There are many ways to do this - for more complex requirements I prefer Power Query but for a simple demo you can go to the Modeling ribbon, choose New table and enter:
Dates = CALENDARAUTO(1)
Next review the Model view and make sure there are no relationships between the new Dates table and your existing ADW_DEFECTS table.
Then I would go to the Modeling ribbon and choose New measure, and copy in this DAX formula:
Active =
SUMX (
Dates,
CALCULATE (
COUNTROWS ( ADW_DEFECTS ),
FILTER (
ADW_DEFECTS,
[Date] >= ADW_DEFECTS[ISSUE_DTTM]
&& [Date] <= ADW_DEFECTS[CLOSE_DTTM]
)
)
)
This basically says for each row in Dates, count how many rows from ADW_DEFECTS are "Active". You have to imagine the Measure formula running in every cell of your output visual. The outer SUMX will calculate a total for all Dates by summing the Date-level results - e.g. how many "Days Active".
Finally add a Table visual to the Report view. Add the Date column from the Dates table and the Active measure. Turn off the Totals if you don't want to show that.
It doesn't produce the extra rows with zeros, e.g. 20/11/2019, but they seem arbitrary. If they are actually required, just add + 0 to the measure formula and filter the Date column on the Table visual.
Related
I have a situation in which I am using Microsoft Power BI. I have a source table (called Couriers), with a range of weights (MinWeight to MaxWeight) for any given combination of Courier and Country, along with the Freight value.
I need to develop a new TABLE (called Couriers_FlattenedData) in Power BI , in which, I get a row for each value between the MinWeight and MaxWeight.
For example, if the minimum weight to maximum weight reads as 0 and 5 for FedEx Australia, I need 5 rows from 1 to 5.
I need these 4 columns in the new Couriers_FlattenedData table - Courier, Country, Weight, Freight. The Weight column is converted to rows based on the range in the source table.
I am trying to derive the new table, in both DAX as well as using the backend Power Query Editor (using M language). I would like to get both ways to develop this new table.
I tried something like this in DAX, but not able to get a solution.
Couriers_FlattenedData = SELECTCOLUMNS (
GENERATE (
'Couriers', GENERATESERIES (
CALCULATE(DISTINCT(Couriers[MinWeight])+1),
CALCULATE(DISTINCT(Couriers[MaxWeight]))
)
),
"Courier", Couriers[Courier],
"Country", Couriers[Country],
"Freight", Couriers[Freight]
)
Can someone correct the above DAX expression, which misses the Weight column ? Or even provide a solution using variables?
And also a step by step solution using the Power Query Editor of Power BI ?
DAX Solution:
Couriers_FlattenedData = SELECTCOLUMNS (
GENERATE (
Couriers,
GENERATESERIES(Couriers[MinWeight] + 1, Couriers[MaxWeight])
),
"Courier", Couriers[Courier],
"Country", Couriers[Country],
"Weight", [Value],
"Freight", Couriers[Freight]
)
Query Editor solution:
Duplicate the Couriers table (in the Query Editor), then go to Advanced Editor of the Query Editor, then paste this:
let
Source = Couriers,
WeightList = Table.CombineColumns(Source,{"MinWeight", "MaxWeight"},each {_{0}+1.._{1}},"Weight"),
ExpandWeightList = Table.ExpandListColumn(WeightList, "Weight"),
ChangedType = Table.TransformColumnTypes(ExpandWeightList,{{"Weight", Int64.Type}})
in
ChangedType
Rename the table as Couriers_FlattenedData
The interaction between two slicers in Power BI gives me output with AND condition.
Example: If I selected the year 2020 and company ABC, the output would be all the data from company ABC in the year 2020.
But I want the two slicers to work with OR condition.
I have used this Dax
Include = (MAX(Table1[Column1]) = SELECTEDVALUE(Col1[Column1])) +
(MAX(Table1[Column2]) = SELECTEDVALUE(Col2[Column2]))
But the problem with above Dax I have not selected anything in slicer ( ALL by default) it is showing me a blank visual. What am I doing wrong?
let me guess you have a table "or_slicer_main_table" with Year, Company and some other columns. Now create 2 new table where the first one will contain the distinct list of Year from table "or_slicer_main_table" and the second one will contain distinct Company list from that same table.
New custom Table 1:
or_slicer_year_list =
SELECTCOLUMNS(
'or_slicer_main_table',
"YEAR", 'or_slicer_main_table'[year]
)
New custom Table 2:
or_slicer_company_list =
SELECTCOLUMNS(
'or_slicer_main_table',
"company", 'or_slicer_main_table'[company]
)
Do not establish any relation between those 3 tables.
Step-1: Create Year slicer using the newly created "or_slicer_year_list" table.
Step-2: Create Company slicer using the newly created "or_slicer_company_list" table.
Step-3: Create these following 5 measures in your table "or_slicer_main_table"
1.
year_current_row = max('or_slicer_main_table'[year])
2.
year_selected_in_slicer = SELECTEDVALUE(or_slicer_year_list[YEAR])
3.
company_current_row = max('or_slicer_main_table'[company])
4.
company_selected_in_slicer = SELECTEDVALUE(or_slicer_company_list[company])
5.
show_hide =
if(
[year_selected_in_slicer] = [year_current_row]
|| [company_selected_in_slicer] = [company_current_row],
1,
0
)
Now you have all instruments ready for play. Create your visual using columns from the table "or_slicer_main_table"
Final Step: Now just add a visual level filter for the measure "show_hide" and set value will show only when "show_hide = 1".
The final output will be something like below image-
Can you try using "IN VALUES" instead of "SELECTEDVALUE"
So your DAX should be
Include = (MAX(Table1[Column1]) IN VALUES (Col1[Column1])) +
(MAX(Table1[Column2]) IN VALUES (Col2[Column2]))
SELECTEDVALUE function returns the result only if single value is selected in slicer in case of multiple selection it will return Blank(). Thats in the case when nothing is selected (which is similar to all selected) has multiple values in set and so SELECTEDVALUE fucntion will return Blank(). This can be handled by using "IN VALUES" function which can return a set of all selected values.
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-
I'm building a PowerBI report from a dataset that contains start and end dates. I'd like to filter the dataset based on rows that would encompass a selected date in another table.
The screenshot here shows a sample. I want to click a date in the table on the right and have the table on the left filtered where the selected date is between the start & end date.
I've attempted several different things using columns and measures, but I haven't been able to nail it down. I also attempted to create a new table from a DAX expression that references the selected date, but that caused errors.
How can I dynamically filter the dataset based on the selected date being between the start and end date values?
Create a measure to check whether a row overlaps the selected date range:
Date Included =
IF (
FIRSTNONBLANK ( Table1[Start Date], 1 ) <= MAX ( 'Calendar'[Date] ) &&
FIRSTNONBLANK( Table1[End Date], 1 ) >= MIN ( 'Calendar'[Date] ),
"Include",
"Exclude"
)
Add this Measure as a filter on your visualisation, where Date Included is Include
Now you can filter your Calendar table ( to single value, or range), and only overlapping rows from your fact table will be displayed.
See https://pwrbi.com/so_55925954/ for worked example PBIX file
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.