PowerBI Additive Slicers (Applied to whole Report) - powerbi

I have a quite niche problem in regards to PowerBI slicer operations.
I wish to filter the data based on two different slicers.
For example I have two different slicers;
A list of categories, say Genre of Films; and
A list of all film directors
I wish to filter the data shown in the PowerBI report based on an OR condition between the two slicers.
For instance, I wish to filter based on all Horror films OR films directed by Quentin Tarantino. So this list would show all Horror films in my database and all films directed by Quentin Tarantino (that are not necessarily Horror films).
I presume that I will need to write some form of DAX code for this but through substantive searching I have not come across this particular problem.
Let me know if you need any further info.
Regards,
Josh

Let's suppose you have a table called IMDB like following
| tbl-IMDB |
|--------------------------|---------|----------------------|
| Name | Genre | Director |
|--------------------------|---------|----------------------|
| The Shawshank Redemption | Drama | Frank Darabont |
| The Godfather | Crime | Francis Ford Coppola |
| The Dark Knight | Action | Christopher Nolan |
| 12 Angry Men | Drama | Sidney Lumet |
| Schindler's List | History | Steven Spielberg |
| Pulp Fiction | Crime | Quentin Tarantino |
and two more disconnected table for slicers as following
| tbl-Director |
|----------------------|
| Director |
|----------------------|
| Frank Darabont |
| Francis Ford Coppola |
| Christopher Nolan |
| Sidney Lumet |
| Steven Spielberg |
| Quentin Tarantino |
|tbl-Genre|
|---------|
| Genre |
|---------|
| Drama |
| Crime |
| Action |
| History |
the data model looks like this
Now, if I understood your question correctly, when you select {Crime,Action} from Genre and {Sidney Lumet, Steven Spielberg} from Director it should return 5 instances; i.e. leaving only the first instance of -The Shawshank Redemption
In order to get there, first all the slicer combinations are needed to be taken into account and what they can do the table.
a. all the slicer values are selected in Director and Genre slicer - DAX to return the full table.
b. none of the slicer values are selected in Director and Genre slicer - DAX to return the full table because the default behavior of DAX is to return the full table if nothing is selected in the slicer.
c. partial values are selected in Genere and nothing/everything is selected in Director and vice versa -
DAX is to return the full table for example, if Drama is selected (2 instances) + nothing/everything is selected in Director (6 instnaces) -> total 6 instances
d. partial values are selected in both Genere and Director - DAX to return the addition of the instances according to the slicer selection - ;e,g. Drama=2+Sideney Lumet=1 => total 3 instances
The above logic can be incorporated in a DAX measure like following
Measure =
VAR _genre = --- what are the genres selected from Genere Slicer and find them in IMDB tbl
MAXX (
FILTER ( IMDB, IMDB[Genre] IN ALLSELECTED ( Genre[Genre] ) ),
IMDB[Genre]
)
VAR _director = --- what are the directors selected from Director Slicer and find them in IMDB tbl
MAXX (
FILTER ( IMDB, IMDB[Director] IN ALLSELECTED ( Director[Director] ) ),
IMDB[Director]
)
VAR _genreCountALL = -- what are the total count of genere from Genre tbl regardless of the slicer seletion
CALCULATE ( COUNT ( Genre[Genre] ), ALL ( Genre ) )
VAR _directorCountALL = -- what are the total count of director from Director tbl regardless of the slicer seletion
CALCULATE ( COUNT ( Director[Director] ), ALL ( Director ) )
VAR _genreCountSELECT = -- what are the total count of genere from Genre tbl according to slicer seletion
COUNT ( Genre[Genre] )
VAR _directorCountSELECT = -- what are the total count of director from Director tbl according to slicer seletion
COUNT ( Director[Director] )
VAR _slice = --- if Genere and/Director slicer are both selected then return the max else in every other instances it is a full tbl
SWITCH (
TRUE (),
_genreCountALL <> _genreCountSELECT
&& _directorCountALL <> _directorCountSELECT,
CALCULATE (
MAX ( IMDB[Name] ),
FILTER ( IMDB, IMDB[Director] IN { _director } || IMDB[Genre] IN { _genre } )
),
CALCULATE ( MAX ( IMDB[Name] ) )
)
RETURN
_slice
and put a visual level filter like following
The measure produces following
nothing is selected - returns full table
partial values are selected in only one slicer- returns full table
partial values are selected in all available slicers - returns the sliced table

Related

PowerBi measure and summarize row for report

I am new to PowerBi.
What I am trying to do is summarize an email marketing report for open, clicks with the exported data. The data looks like
| Campaign Name | Email Address | Event Type |
|:---------------: |:---------------------:| :--------: |
| Test Campaign | test#example.com | Open |
| Test Campaign | test#example.com | Open |
| Test Campaign | test123#example.com | Open |
| Test Campaign 2 | test#example.com | Open |
| Test Campaign 2 | test#example.com | Open |
| Test Campaign 2 | test1234#example.com | Open |
| Test Campaign 2 | test#example.com | Clicked |
| Test Campaign 2 | test#example.com | Clicked |
I want to calculate each event type that is unique for each email address and summarize it by the campaign name. There can be duplicate email address if someone opens email twice. And there are multiple different campaign names
I want PowerBI to be able to take the above data and summarize it as such with a measure formula :
Campaign Name
Open
Click
Test Campaign
2
0
Test Campaign 2
2
1
Any suggestions? I can't get it to summarize by campaign name.
Clicks = Calculate(DistinctCount('table'[column]),'table2 for event type'[event type] = "click"))
You can do it in DAX very quickly.
In PowerBI, you create two new measures:
Please note that I've used 'Table' as the table name, replace it with the actual name of your table.
'Table'[Open_Nb] =
CALCULATE(
DISTINCTCOUNT('Table'[Email Address ]),
'Table'[Event Type] = "Open"
)
and
'Table'[Clicked_Nb] =
CALCULATE(
DISTINCTCOUNT('Table'[Email Address ]),
'Table'[Event Type] = "Clicked"
)
Then you can use this table two ways. You can create a new Table visual and drag and drop :
Campaign Name
Measure Open_Nb
Measure Clicked_Nb
Or, directly as a new table with the expression :
ADDCOLUMNS(
SUMMARIZE(
'Table',
'Table'[Campaign Name ]
),
"Open",
'Table'[Open_Nb],
"Click",
'Table'[Clicked_Nb]
)

DAX Power BI - Show latest record according to date filter

I have 2 tables in Power BI model, [Date] and [Quota]
'Date'[YYYYMM] is used as a period filter on the report
I want to show the latest quota according to the maximum date of the chosen period
How can I write it in DAX / Calculated Column?
Example:
If I select YYYYMM = "202005" in filter.
Below table should show:
|product|quota|
|A |10 |
|B |20 |
Quota Table:
|product|quota|effectiveDate|
|A |10 |2020-01-01 |
|B |20 |2020-01-01 |
|A |25 |2021-01-01 |
Date Table:
|Date |YYYYMM|
|2020-01-01|202001|
|2020-01-02|202001|
...
|2021-06-09|202106|
To get the latest quota, in SQL it would be:
SELECT TOP (1) q.Quota
FROM [Quota] q
LEFT JOIN [Date] d on d.[Date] >= q.effectiveDate
ORDER BY q.effectiveDate desc
Measure in DAX:
FlagToFilter =
var __ChoicedDate = CALCULATE(MAX(DateTable[Date ]), FILTER(ALL(DateTable), SELECTEDVALUE(DateTable[YYYYMM]) = DateTable[YYYYMM] ))
var __MaxFor = CALCULATE(max(QuotaTable[effectiveDate]), FILTER(ALL(QuotaTable), QuotaTable[effectiveDate] <= __ChoicedDate && SELECTEDVALUE(QuotaTable[product])= QuotaTable[product] ))
return
CALCULATE( countrows(VALUES(QuotaTable[product])), FILTER(ALL(QuotaTable[effectiveDate]), SELECTEDVALUE(QuotaTable[effectiveDate]) = __MaxFor))

Power BI - Using a slicer on a Matrix Grand total

I have been working on this Power BI Report and would like some assistance with a slicer used for a matrix.
I need the slicers "MB Data Used", "Calls Made", and "SMS Sent" to slice the Matrix Grand Total fields (on the far right) instead of the value fields. My current slicers work great on the value fields.
Data is filled by a table:
----------------------------------------------------
|ph_id | month | data_used | calls_made | sms_sent |
| 1 | 1/1/19| 123 | 0 | 33 |
| 2 | 1/1/19| 87 | 22 | 0 |
| 3 | 1/1/19| 0 | 0 | 0 |
| 1 | 1/2/19| 0 | 55 | 33 |
| 2 | 1/2/19| 87 | 22 | 77 |
| 3 | 1/2/19| 0 | 0 | 0 |
----------------------------------------------------
Which links to a few others to get related data.
My goal is to be able to see which phone numbers have had no data/call/sms use over the last X months instead of just filtering the ones which contain a 0. In this scenario, when the slicers are all set to 0 and the date range is set 1/1/19-1/2/19, only ph_id 3 should show.
Edit:
W.B. - see this image
You need to use another, unrelated table for your slicers. The best way to create such table is to use the what-if parameter option in the modelling tab (assuming you have any recent version of PBI Desktop).
Or, if you want to base the slicer on number of calls from actual data, you would create the slicer table using New table option and the following formula: CallSlicer = GENERATESERIES(MIN(Data[calls_made]), MAX(Data[calls_made]), 1). The one at the end indicates the step, so you can adjust it, if you want your users to use the slicer in, for instance, increments of 10 or 20.
Now, when you use the generated CallSlicer column, which looks like this:
You will be able to filter your results like this: Your filtered measure = CALCULATE([your_measure], FILTER(Data, Data[calls_made] >= MIN(CallSlicer[CallSlicer]) && Data[calls_made] <= MAX(CallSlicer[CallSlicer]))). You then use your filtered measure in the matrix visual.
EDIT:
Here's a working sample: https://1drv.ms/u/s!AmqvMyRqhrBpgtRGGbJ6w-b66uBENQ?e=67JduS
I've updated the sample - now it shows 2 scenarios. One table reacts to the slicer at individual cell level, the other one at the grand total level.
The key to have the first table working is shown above, below is a solution for the second table, that filters rows at the grand total level:
Create a measure that will show sum for all dates/months, as an example:
CallSumTotal =
VAR tab =
FILTER (
CALCULATETABLE (
SUMMARIZE ( Data, Data[id], "calls_made", SUM ( Data[calls_made] ) ),
ALLSELECTED ( Data[month] )
),
[calls_made] >= MIN ( CallSlicer[Value] )
&& [calls_made] <= MAX ( CallSlicer[Value] )
)
RETURN
SUMX ( tab, [calls_made] )
Now in the matrix use a regular sum measure, but create a visual level filter for CallSumTotal and set it to is not blank

Power BI DAX Running Total with Multiple Filters

I created a monthly rolling amount which sums invoices for the month and displays the summary on the first day of the month. This code works fine, except it displays the running total for all months of the year, even when there is no data (or the month hasn't arrived yet, like Dec 2020).
Running Total InvoiceTotals =
CALCULATE(
[InvoiceTotals],
FILTER(
CALCULATETABLE(
SUMMARIZE('Date', 'Date'[YearMonthSort], 'Date'['Date'[FirstDayOfMonth]]),
ALLSELECTED('Date')
),
ISONORAFTER(
'Date'[YearMonthSort], MAX('Date'[YearMonthSort]), DESC,
'Date'['Date'[FirstDayOfMonth]], MAX('Date'['Date'[FirstDayOfMonth]]), DESC
)
)
)
--Result
|---------|------------------|---------------|
| Date | Invoice Total | Running Total |
|---------|------------------|---------------|
|6/1/2020 | 500 | 500 |
|---------|------------------|---------------|
|7/1/2020 | 700 | 1200 |
|---------|------------------|---------------|
|8/1/2020 | | 1200 |
|---------|------------------|---------------|
|9/1/2020 | | 1200 |
|---------|------------------|---------------|
I'd like to get rid of the last two rows, which I can with the following code, but I can't combine this filter with the filters above.
Running Total InvoiceTotals =
CALCULATE(
[InvoiceTotals],
FILTER(
'OrderHeader','OrderHeader'[InvoiceTotals] > 0
)
)
How can I combine these filters?
EDIT: I replaced older code with my newest set of code.
As discussed in the commentary section, you can try inserting "Invoice Total" column into a Visual Filter (right-hand side pane) and filter out blank "Invoice Total" values. Thanks to this solution you will avoid editing your measure.

PowerBI DAX – subquery to the same table

I have a table, let's call it Products with columns:
Id
ProductId
Version
some other columns…
Id column is the primary key, and ProductId groups rows. Now I want to view distinct values of ProductId where Version is highest.
I.e. From data set:
Id | ProductId | Version | ...
100 | 1 | 0 | ...
101 | 2 | 0 | ...
102 | 2 | 1 | ...
103 | 2 | 2 | ...
I need to get:
Id | ProductId | Version | ...
100 | 1 | 0 | ...
103 | 2 | 2 | ...
In SQL I would write:
SELECT Id, ProductId, Version, OtherColumns
FROM Products p1
WHERE NOT EXISTS
(SELECT 1
FROM Products p2
WHERE p2.ProductId = p1.ProductId
AND p2.Version > p1.Version)
But I have no idea how to express this in DAX. Is this approach with subqueries inapplicable in PowerBI?
Another approach is to first construct a virtual table of product_ids and their latest versions, and then use this table to filter the original table:
EVALUATE
VAR Latest_Product_Versions =
ADDCOLUMNS(
VALUES('Product'[Product_Id]),
"Latest Version", CALCULATE(MAX('Product'[Version])))
RETURN
CALCULATETABLE(
'Product',
TREATAS(Latest_Product_Versions, 'Product'[Product_Id], 'Product'[Version]))
Result:
The benefit of this approach is optimal query execution plan.
You can use SUMMARIZECOLUMNS to group ProductId and MAX Version.
Then use ADDCOLUMNS to add the corresponding Id number(s), using a filter on the Products table for the matching ProductId and Version. I've used CONCATENATEX here, so that if multiple Id values have the same Product / MAX Version combination, all Id values will be returned, as a list.
EVALUATE
ADDCOLUMNS (
SUMMARIZECOLUMNS (
Products[ProductId],
"#Max Version",
MAX ( Products[Version] )
),
"#Max Version Id",
CONCATENATEX (
FILTER (
Products,
Products[Version] = [#Max Version] && Products[ProductId] = EARLIER ( Products[ProductId] )
),
Products[Id],
","
)
)