How to do a cumulative with a duplicate value - powerbi

[http://uupload.ir/view/v2t_capture.png] We have 2 tables with 2 columns:
table location ->column ->city ,province
table sales column ->actual sales
table location : table sales
province city id sales
tehran eslamshar 1 100
tehran rey 2 500
hamedan tefresh 3 500
esfahan esahan 4 400
gilan rasht 5 400
gilan rar 6 900
I want to calculate 80% of total sales in each city and each province
it means if we have 2800 total sale in whole province, we want just show province that cut off 80 % of sales:
I use this measure but it has one problem that duplicate value does not count in a cumulative sum.
If I have two same sale like 900 it does not count in cumulative and it ignores it.
Cumulative Total =
IF (
NOT ( ISBLANK ( [sales] ) ),
CALCULATE (
[sales],
FILTER (
ALL ( DimLocation[Province] ),
CALCULATE ( [sales], VALUES ( DimLocation[Province] ) ) <= [sales]
)
)
)
Expected Result:
sales cumulative
tehran 800 800
shiraz 200 1000
ghom 200 400
markazi 300 500
Output:
tehran 800 800
shiraz 200 1000
ghom 200 1000
markazi 300 500

For a running total you could create this measure:
RT Province Sales :=
VAR _current = SELECTEDVALUE ( DimLocation[Province] )
RETURN
CALCULATE (
[Sales],
DimLocation[Province] <= _current ,
ALL ( DimLocation[Province] )
)

Related

PowerBI Get cumulative value for each day and reset value each year

I want to achive do cumulate values per day per product and reset the value for every new year.
What I have:
Date
productID
value
01.01.2022
1270
30000
01.01.2022
1280
200000
02.01.2022
1280
 -50
01.02.2022
1280
100
01.02.2022
1280
200
01.02.2022
1270
-20
01.03.2022
1270
80
29.12.2022
1270
100
29.12.2022
1280
10
31.12.2022
1270
35
31.12.2022
1270
5
01.01.2023
1270
50000
01.01.2023
1280
100000
04.01.2023
1270
50
06.01.2023
1280
-100
Value should be calculated cumulative per day with a fresh start from each year and per productID.
What I want as a measure is Cumulative Per Year.
Date
productID
value
Cumulative per Year
01.01.2022
1270
30000
30000
01.01.2022
1280
200000
200000
02.01.2022
1280
 -50
199950
01.02.2022
1280
100
200050
01.02.2022
1280
200
200250
01.02.2022
1270
-20
29980
01.03.2022
1270
80
30060
29.12.2022
1270
100
30160
29.12.2022
1280
10
200260
31.12.2022
1270
35
30195
31.12.2022
1270
5
30200
01.01.2023
1270
50000
50000
01.01.2023
1280
100000
100000
04.01.2023
1270
50
50050
06.01.2023
1280
-100
99900
What I tried:
Cumulative per Year =
VAR varProductID = SELECTEDVALUE(MyTable[productID])
VAR varYear = SELECTEDVALUE(MyTable[date])
CALCULATE(SUM(MyTable[Value],
FILTER(MyTable,
varProductID = MyTable[productID] &&
varYear = MyTable[date]
)
What I also tried is STARTOFYEAR() and ENDOFYEAR() to know when the cumulative should reset but I not meant to work with selectedvalue() also for some reason MyTable[date].Year wont work.
Thanks for any help.
this should work...
Cumulative per Year =
VAR _date =
SELECTEDVALUE ( 'Table'[Date] )
VAR _id =
SELECTEDVALUE ( 'Table'[productID] )
RETURN
CALCULATE (
SUM ( 'Table'[value] ),
FILTER (
ALL ( 'Table' ),
_id = 'Table'[productID]
&& 'Table'[Date].[Year] = YEAR ( _date )
&& 'Table'[Date] <= _date
)
)
What you could try is TOTALYTD time intelligence measure and drop it in as a last column and see if it works.
It would look something like this:
Cummulative Total=TOTALYTD(SUM(Table[Value]),DateTable[Date])
If you don't have it already you will need to create a date dimension table, see link here for a steps on how to create a Calendar table > https://radacad.com/all-in-one-script-to-create-calendar-table-or-date-dimension-using-dax-in-power-bi
Create a relationship between calendar table date > your table date and then use cummulative total measure. Always best to have date dimension table.

Dax filter table desc with running total

I have table with profit, dev_num
I want to filter that table desc by profit column where running sum of dev_num =<30
i mean sth like this:
Profit | Dev_num
1000000 10
100000 10
340000 6
240000 4
do you know maybe how i should build my measure for this calculated table ?
Your DAX can be like below :
EVALUATE
FILTER (
ADDCOLUMNS (
VALUES ( 'yourTable'[Profit] ),
"Dev_num", CALCULATE ( SUM ( 'yourTable'[Dev_num]) )
),
[Dev_num] <= 30
)
ORDER BY 'yourTable'[Profit]

DAX formula for sales sum for each day of previous n days

Given a table FACT in Power BI with three columns Date, Category and Sales I am looking for a DAX function that for each day returns the sum of the sales of its previous n days.
Lets assume n = 2, that means for day 01/04/2020 my measure should return the sum of the sales of the days 01/02/2020 and 01/03/2020.
Here is a small example:
Date Category Sales
01/01/2020 A 1
01/01/2020 B 3
01/02/2020 B 2
01/03/2020 B 1
01/04/2020 A 0
01/05/2020 B 10
01/06/2020 B 7
What I want is
Date MyMeasure
01/01/2020 0
01/02/2020 4
01/03/2020 6
01/04/2020 3
01/05/2020 1
01/06/2020 10
I later would then like to use the Category as a filter and in my case n is 365.
I tried the following
MyMeasure =
VAR FROM_DATE =
DATEADD ( FACT[DATE], 0, DAY )
VAR SALES_365 =
CALCULATE (
SUM ( FACT[SALES] ),
DATESINPERIOD ( FACT[DATE], FROM_DATE, 365, DAY )
)
RETURN
SALES_365
but got an error that a table of multiple values was supplied where a single value was expected.
The error was in the variable FROM_DATE, in which DATEADDD returned a column of dates and not a single value.
That said you don't really need a variable for that, you can just use a simple subtraction and DATESBETWEEN(DateCol, DateFrom,DateTo)
MyMeasure =
CALCULATE (
SUM ( FACT[SALES] ),
DATESBETWEEN('FACT'[DATE],MAX(FACT[DATE])-2,MAX(FACT[DATE])-1)
)

How to calculate yearly average per product?

I am new to Power BI and the language DAX. Therefore, I find it hard to do simple yearly average across different products.
I have tried a different combination of the functions AVERAGEX and CALCULATE without any luck so far.
My table:
Date | Product | Value
2014-05-06 Cheese 10
2014-08-11 Cheese 12
2015-04-11 Cheese 9
2014-01-22 Milk 4
2014-12-24 Milk 8
The output I try to create:
Date | Product | Value | Yearly_AVG
2014-05-06 Cheese 10 11
2014-08-11 Cheese 12 11
2015-04-11 Cheese 9 9
2014-01-22 Milk 4 6
2014-12-24 Milk 8 6
Lastly, I do not have a calendar table in the dataset.
For a measure, you can write something like this:
YearAvg =
VAR CurrProduct = VALUES ( Sales[Product] )
VAR CurrYear = YEAR ( MAX ( Sales[Date] ) )
RETURN
CALCULATE (
AVERAGE ( Sales[Value] ),
ALLSELECTED ( Sales ),
Sales[Product] IN CurrProduct,
YEAR ( Sales[Date] ) = CurrYear
)

Sum with filter on DAX/PowerBI

I need to sum each month the ID's amounts that meet the next criterias:
Date End < Agreement date
Date End < Month to show
I have tried with filter, sum and others options, but the result is the same.
My datas are:
ID START DATE AGREEMENT DATE END DATE AMOUNT
1 09/15/2018 01/01/2019 02/20/2019 100
2 09/20/2018 01/15/2019 12/01/2019 100
3 10/01/2018 03/01/2019 12/01/2019 100
4 10/01/2018 03/20/2019 05/01/2019 100
5 11/10/2018 07/10/2019 100
6 09/15/2018 04/05/2019 05/01/2019 100
7 10/01/2018 06/10/2019 05/01/2019 100
8 10/20/2018 07/11/2019 04/10/2019 100
9 11/11/2018 08/01/2019 100
10 11/01/2018 06/01/2019 04/10/2019 100
This is my DAX code:
Result =
CALCULATE (
SUM ( 'Report Diario'[Amount] );
USERELATIONSHIP ( Calendario[Date]; 'Report Diario'[End Date] );
FILTER (
'Report Diario';
'Report Diario'[End Date] < 'Report Diario'[Agreement date]
)
)
finally, I have created a new table and the next calculated column works:
Result = CALCULATE(SUM('Report Diario'[Amount]);FILTER('Report Diario';AND(Calendario[Date]<'Report Diario'[Agreement Date];AND(Calendario[Date]>'Report Diario'[End Date];NOT(ISBLANK('Report Diario'[End Date]))))))