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],
","
)
)
Related
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
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))
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
I am working on a fitness dashboard which provides a 'performance' score across different exercises.
In the provided screenshot, you can see a table which shows me an averaged daily 'performance' result for 'deadlifts' for example.
Obviously, I don't do these every day, so there are gaps between the dates for which I have records.
What I want is to be able to produce a '% improvement vs last recorded day' measure. Is it possible to create a measure that runs a comparison of this sort?
Assuming your table look like this
Table
+-------------------+-------------+
| Date | Performance |
+-------------------+-------------+
| 02 September 2020 | 20 |
+-------------------+-------------+
| 08 September 2020 | 20 |
+-------------------+-------------+
| 14 September 2020 | 20 |
+-------------------+-------------+
| 25 September 2020 | 32 |
+-------------------+-------------+
| 26 October 2020 | 36 |
+-------------------+-------------+
I have created a calculation that would scans the table for the last available date besides the
Steps:
Get the current value on each row. For example, 1st row is 2 of September. The value is stored in _SelectedValue
Use MAX to calculate the nearest value excluding the _SelectedValue. Save the result in MaxDate.
Using the 3rd step, filter the table to get the sum of performance for MaxDate.
Get the Percentage difference
Performance Calculation =
VAR _SelectedValue =
SELECTEDVALUE ( 'Table'[Date] )
VAR MaxDate =
CALCULATE (
MAX ( 'Table'[Date] ),
FILTER ( ALLSELECTED ( 'Table' ), 'Table'[Date] < _SelectedValue )
)
VAR PreviousPerformance =
CALCULATE (
SUM ( 'Table'[Performance] ),
FILTER ( ALLSELECTED ( 'Table' ), 'Table'[Date] = MaxDate )
)
VAR CurrentPerformance =
SUM ( 'Table'[Performance] )
VAR Result =
IF (
ISBLANK ( MaxDate ),
BLANK (),
DIVIDE ( CurrentPerformance - PreviousPerformance, PreviousPerformance )
)
RETURN
Result
Output
In my Power BI I have the following table
What I want is that when you create a graph and have a filter, the ranking column is modified. An example would be that when I change the ranking and select center, I get it like this:
site | region | nro_trabajos | ranking
----------+--------+--------------+------
OCCORO | centro | 58 | 1
PILCOMAYO | centro | 59 | 2
SICAYA | centro | 71 | 3
.
.
etc
I generate a table that gives me the following ranking. but has ties
the measure is rank_measure, as this image shows
that I can add in this measure so that the tiebreaker is with the alphabetical order of the "site" column and that it still has the region filter?
A calculated column cannot be dynamic with respect to slicers or filters, but you definitely can create a dynamic ranking measure.
ranking =
RANKX (
ALLSELECTED ( Table1 ),
Table1[nro_trabajos],
MAX ( Table1[nro_trabajos] )
)
Edit:
If you want to do tiebreaking, I think the simplest way is to first create a UniqueRank calculated column:
UniqueRank =
RANKX (
Table1,
FORMAT ( Table1[nro_trabajos], "0000" ) & Table1[region] & Table1[site]
)
You put each column that makes up a unique row in the order you want to rank by, formatting any numbers by padded with sufficient zeros so that the numeric ordering matches the alphabetic ordering.
Then simply use this calculated column UniqueRank instead of nro_trabajos in the measure I gave above:
ranking =
RANKX (
ALLSELECTED ( Table1 ),
Table1[UniqueRank],
MAX ( Table1[UniqueRank] )
)