I need to create a pie or donut chart in power bi that displays the percentage of the selected items from a slicer in relation to the grand total in the same visual while keeping the legends.
Ex:
---------------------------
| Id | Product | Category |
| 1 | P1 | A |
| 2 | P2 | A |
| 3 | P3 | A |
| 4 | P4 | B |
| 5 | P5 | B |
| 6 | P6 | B |
| 7 | P7 | C |
| 8 | P8 | C |
| 9 | P9 | D |
---------------------------
I have a slicer with Category A,B,C,D IF I select A, B; I want the pie chart to show A, B then combines the remaining not selected values as the rest of the Pie chart so it shows three categories A, B, remainder "C&D" together.
I have the following Measures to count the selected and not selected values.
But when using two measures in a pie or donut chart it displays them as subcategories.
Count not Selected Products =
CALCULATE (
COUNT ( Products[Product] ),
FILTER (
ALL ( Products[Category] ),
NOT Products[Category] IN VALUES ( Products[Category] )
)
)
Count Selected =
CALCULATE (
COUNT ( Products[ID] ),
FILTER ( Products, Products[Category] IN ALLSELECTED ( Products[Category] ) )
)
Is it possible to do it in power bi ?
First, you'll need to define a new table including "Remaining"to use in the pie chart.
Something like this should work:
Pie = UNION ( VALUES ( Products[Category] ), { { "Remaining" } } )
This is just a single column table:
Category
--------
A
B
C
D
Remaining
Put this newly defined column as the Legend for the pie chart and write a measure as follows:
CountProducts =
IF (
SELECTEDVALUE ( Pie[Category] ) = "Remaining",
CALCULATE (
COUNT ( Products[Product] ),
ALL ( Products[Category] ),
NOT ( Products[Category] IN VALUES ( Products[Category] ) )
),
CALCULATE (
COUNT ( Products[Product] ),
FILTER ( Products, Products[Category] IN VALUES ( Pie[Category] ) )
)
)
The first part is exactly what you had and the second part is very similar.
Related
I am looking for a solution to sum previous rows in a calculated column.
Sample data:
product | sales | cumulative
000001 | 2000 | 2000
000001 | 2000 | 4000
000002 | 1500 | 1500
000001 | 2000 | 6000
000002 | 1500 | 3000
Could anyone help me with the DAX please.
This is what I would do
step 1)
I would create a new ordinal column with a sequential number for each product group
index =
RANKX (
FILTER (
Sales,
EARLIER ( Sales[Product] ) = Sales[Product]
),
Sales[Sales],
,
ASC
)
Step 2)
I create the Running Totals column that shows the running totals by group
Running Totals =
CALCULATE (
SUM (Sales[Sales]),
FILTER(
Sales,
Sales[Product] = EARLIER ( Sales[Product])
&& Sales[index] <= EARLIER ( Sales[index])
)
)
this is the result
In Power BI I would like to create a DAX measure that will retrieve the latest string value for specific IDs. Example source table:
Name_ID | Name | DateTime | Value
----------------------------------------------------------
1 | Child_1 | 18.8.2021 12:33:24 | F
32 | Parent_32 | 18.8.2021 11:41:09 | F
13 | Child_1 | 18.8.2021 11:30:58 | E
48 | Parent_48 | 18.8.2021 09:13:11 | F
2 | Child_2 | 17.8.2021 00:09:42 | S
1 | Child_1 | 17.8.2021 23:03:34 | F
48 | Parent_48 | 17.8.2021 21:46:27 | S
6 | Parent_6 | 16.8.2021 17:31:26 | S
.
.
.
specific parents IDs for example here are 6, 32 and 48, so the result should be something like this:
Name_ID | Name | DateTime (of last execution) | Value
------------------------------------------------------------------------------
32 | Parent_32 | 18.8.2021 11:41:09 | F
48 | Parent_48 | 18.8.2021 09:13:11 | F
6 | Parent_6 | 16.8.2021 17:31:26 | S
The result table I'm trying to get is only parents latest appearance and retrieving the whole row or just Value from last column.
This seems so easy in theory and on paper but I just can't seem to get it in DAX I have tried with various calculate formulas but without any result worth mentioning .
I'm beginner in Power Bi and any help would be very appreciated!
You can use a measure like this one, where we check Max Date per Name:
Flag =
var MaxDatePerName = CALCULATE(max(Sheet3[DateTime]), FILTER(ALL(Sheet3), SELECTEDVALUE(Sheet3[Name]) = Sheet3[Name]))
return
if( MaxDatePerName = SELECTEDVALUE(Sheet3[DateTime]) && LEFT(SELECTEDVALUE(Sheet3[Name]),6) = "Parent", 1, BLANK())
With RANKX
Measure2 =
VAR _0 =
MAX ( 'Table 1'[DateTime] )
VAR _00 =
MAX ( 'Table 1'[Name] )
VAR _1 =
CALCULATE (
RANKX (
FILTER ( ALL ( 'Table 1' ), 'Table 1'[Name] = _00 ),
CALCULATE ( MAX ( 'Table 1'[DateTime] ) ),
,
DESC
)
)
VAR _2 =
IF ( _1 = 1 && CONTAINSSTRING ( _00, "Parent" ) = TRUE (), _0, BLANK () )
RETURN
_2
I have this table:
Order | Total | FirstPayment | Months
1 | 1000 | 2021-01-01 | 2
2 | 600 | 2021-02-01 | 3
And I need to create a another table with the installments, like this:
Month | Order | Value
2021-01-01 | 1 | 500
2021-02-01 | 1 | 500
2021-02-01 | 2 | 200
2021-03-01 | 2 | 200
2021-04-01 | 2 | 200
So, I want to create a child table with one row for each month of payment.
Please, can you help?
As per my comment, I would actually do it like this:
Create dates table that spans all dates between two ranges. You could actually filter it to contain only relevant dates for better performance, but I didn't bother (this is a table formula):
Payments = CALENDAR(MIN(Orders[FirstPayment]), MAXX(Orders, EDATE(Orders[FirstPayment], Orders[Months])))
Create a measure that would show appropriate values for relevant dates:
Payment amount =
SUMX (
Payments,
VAR d =
DAY ( Payments[Date] )
RETURN
SUMX (
FILTER (
Orders,
DAY ( Orders[FirstPayment] )
== d
&& Payments[Date] <= EDATE ( Orders[FirstPayment], Orders[Months] -1 )
&& Payments[Date] >= Orders[FirstPayment]
),
[Total] / [Months]
)
)
The result - based on Order from Orders table and Date from Payments table:
EDIT
Of course, it is also possible to do what you asked. You have to combine the two formulas to create a calculated table like this (below is a table formula that you apply when you select New table):
Installments =
SELECTCOLUMNS (
FILTER (
CROSSJOIN (
CALENDAR (
MIN ( Orders[FirstPayment] ),
MAXX ( Orders, EDATE ( Orders[FirstPayment], Orders[Months] ) )
),
Orders
),
[Date] >= [FirstPayment]
&& DAY ( [Date] ) = DAY ( [FirstPayment] )
&& [Date]
<= EDATE ( [FirstPayment], [Months] - 1 )
),
"Date", [Date],
"Order", [Order],
"Value", [Total] / [Months]
)
How to select the row with a max value per category in DAX/SSAS? Suppose we have table:
+----------+-------+------------+
| Category | Value | Date |
+----------+-------+------------+
| apples | 1 | 2018-07-01 |
| apples | 2 | 2018-07-02 |
| apples | 3 | 2018-07-03 |
| bananas | 7 | 2018-07-04 |
| bananas | 8 | 2018-07-05 |
| bananas | 9 | 2018-07-06 |
+----------+-------+------------+
Desired results are:
+----------+-------+------------+
| Category | Value | Date |
+----------+-------+------------+
| apples | 3 | 2018-07-03 |
| bananas | 9 | 2018-07-06 |
+----------+-------+------------+
From your question it is not clear whether you want to return a table or a measure.
If you need to return a table, then go with this:
calculated_table
:=VAR _categoryfirst=
ADDCOLUMNS (
VALUES ( 'Table'[Category] ),
"MinValue", CALCULATE (MIN ( 'Table'[Value] ),ALL ( 'Table'[Value] ))
)
RETURN
CALCULATETABLE(
SELECTCOLUMNS('Table',
"Category", 'Table'[Category]
"Value", 'Table'[Value]
"Date", 'Table'[Date]
),
KEEPFILTERS (
TREATAS (
_categoryfirst,
'Table'[Category],
'Table'[Value]
)
)
)
Instead, if you want to create a Table visualization with Category, Value, Date showing only the row associated with minimum value I would go with this:
measure:=
VAR _categoryfirst=
CALCULATETABLE(
ADDCOLUMNS (
VALUES ( 'Table'[Category] ),
"MinValue", CALCULATE (MIN ( 'Table'[Value] ),ALL ( 'Table'[Value] ))
),
ALL('Table'[Value]),
ALL('Table'[Date])
)
RETURN
CALCULATE(
MIN('Table'[Value]),
KEEPFILTERS (
TREATAS (
_categoryfirst,
'Table1'[Category],
'Table1'[Value]
)
)
)
How to construct a DAX measure which returns sum of either A or B. The logic is take B if A is empty. So expected results looks like this:
+---+---+----------+
| A | B | Expected |
+---+---+----------+
| 1 | | 1 |
| 1 | | 1 |
| | 2 | 2 |
| 1 | 2 | 1 |
| | 2 | 2 |
+---+---+----------+
| 3 | 6 | 7 |
+---+---+----------+
When I use measure:
Measure = IF(ISBLANK([SUM(tab[A])]), SUM(tab[B]), SUM(tab[A]))
I get 3 for total which is logical but not what I expect.
I'd recommend using a SUMX iterator in this case.
Measure = SUMX ( tab, IF ( ISBLANK ( tab[A] ), tab[B], tab[A] ) )
You might be able to do the following as well:
Measure =
CALCULATE ( SUM ( tab[A] ) ) +
CALCULATE ( SUM ( tab[B] ),
FILTER ( tab, ISBLANK( tab[A] ) )
)