How to create dynamic child table with DAX in PowerBI - powerbi

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]
)

Related

Sum previous rows in column

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

SUM for previous Weeknum in Power BI

Suppose we have a table
weeknum | revenue
------------------
12 | 10000
12 | 10000
12 | 10000
13 | 10000
13 | 10000
13 | 10000
14 | 10000
14 | 10000
I tried to calculate the sum of the revenue for the previous weeknum:
Previous Revenue =
CALCULATE(
SUM(Table1[revenue]),
FILTER(
ALL(Table1[weeknum]),
Table1[weeknum] = Table1[weeknum]-1
)
)
But, it is failed. Any Idea on this one
The statement you are passing to FILTER's FilterExpression needs to refer to the entry from Table1[weeknum] within the current row context.
This can be achieved by replacing
Table1[weeknum] = Table1[weeknum]-1
with
Table1[weeknum] = MIN(Table1[weeknum])-1
though it is perhaps better practice to create a variable, viz:
Previous Revenue :=
VAR ThisWeekNum =
MIN( Table1[weeknum] )
RETURN
CALCULATE(
SUM( Table1[revenue] ),
FILTER(
ALL( Table1[weeknum] ),
Table1[weeknum] = ThisWeekNum - 1
)
)

How do I calculate the number of positive days?

I have the following measure:
no_positive_bets =
COUNTAX(
FILTER(
'belgarath match_',
'belgarath match_'[ogion_pnl] >= 0
),
'belgarath match_'[ogion_pnl]
)
Assuming there is a field called 'belgarath match_'[date] how would I group 'belgarath match_'[ogion_pnl] by day so that I can work out the number of positive days?
Edit:
By way of some sample data:
+-----+------------+-----------+
| id_ | date | ogion_pnl |
+-----+------------+-----------+
| 1 | 01/01/2020 | 100 |
| 2 | 02/01/2020 | 100 |
| 3 | 02/01/2020 | -50 |
| 4 | 03/01/2020 | 100 |
| 5 | 03/01/2020 | -150 |
+-----+------------+-----------+
The current snippet I have will return 3 because three rows are positive. However I would like it to return 2 as the first two days are positive.
Try this it may help..
no_positive_bets = COUNTAX(FILTER('belgarath match_', [ogion_pnl] >= 0), [ogion_pnl])
Create this below Custom Column First-
is possitive =
VAR current_date = your_table_name[date]
VAR current_date_sum =
CALCULATE(
SUM(your_table_name[ogion_pnl]),
FILTER(
ALL(your_table_name),
your_table_name[date] = current_date
)
)
RETURN IF(current_date_sum >= 0, 1, 0)
Now create this below Measure-
number of possitive day =
CALCULATE(
DISTINCTCOUNT(your_table_name[date]),
FILTER(
ALL(your_table_name),
your_table_name[is possitive] = 1
)
)
Now add the measure number of possitive day to a card and the output will be as below-

Show selected and remaining in Power BI pie chart

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.

Lookup previous value based on criteria from another column

Objective:
Get the previous value based on a criteria.
Situation:
I have a table with groups numbered 1,2. I would like to look at the previous value (referring to the previous date) but for each group.
Desired Output:
My output should look like this
+------------+-------+-------+----------------+
| date | group | value | previous value |
+------------+-------+-------+----------------+
| 2019-02-02 | 2 | 50 | 45 |
| 2019-02-02 | 1 | 60 | 80 |
| 2019-01-18 | 2 | 45 | |
| 2019-01-18 | 1 | 80 | |
+------------+-------+-------+----------------+
What I tried:
previous value =
LOOKUPVALUE(
Table[value],
Table[date],
CALCULATE(
MAX(Table[date]),
FILTER(
Table,
Table[group]=EARLIER(Table[group]) && Table[date]<EARLIER(Table[date])
)
)
)
As I understand, you want this as a calculated column, not a measure. Try:
Previous Value =
VAR Current_Date = Table[date]
VAR Previous_Date =
CALCULATE (
MAX ( Table[date] ),
Table[date] < Current_Date,
ALLEXCEPT ( Table, Table[group] )
)
RETURN
CALCULATE (
MAX ( Table[value] ),
Table[Date] = Previous_Date,
ALLEXCEPT ( Table, Table[group] )
)
How it works:
We iterate each record of the Table and store its date in "Current_Date" variable.
For each record, find previous date, which is the max date that is smaller than the date of the record we are iterating. To do that, we need to have access to all dates, not only the date of the current record, so we need to use ALL function. However, since we need to do it by group, we use ALLEXCEPT, which preserves filter for the current group.
Once previous date is found, you can use exactly the same pattern to find previous value - find MAX value where record's date equals previous date, while preserving group filter.