Cumulative of Cumulative measure power bi - powerbi

Hi Team,
I need 2 output.
Output1 = Cumulative of No of Item.
Output2 = Cumulative of Output1.
Please help me.

If you have Date column from where you are picking the Month name, you can try this below 2 custom column code. I just considered first date of each month in the date column, but it will work for any date. this below column will give you cumulative vale per Year to Month-
Note- Based on your Date value, you may need some adjustment in the script. But using this logic, you should achieve your required output.
C1 =
var current_row_date_month = your_table_name[Date].[MonthNo]
return
CALCULATE(
SUM(your_table_name[No Of Item]),
FILTER(
ALLEXCEPT(your_table_name,your_table_name[Date].[Year]),
your_table_name[Date].[MonthNo] <= current_row_date_month
)
)
C2 =
var current_row_date_month = your_table_name[Date].[MonthNo]
return
CALCULATE(
SUM(your_table_name[C1]),
FILTER(
ALLEXCEPT(your_table_name,your_table_name[Date].[Year]),
your_table_name[Date].[MonthNo] <= current_row_date_month
)
)
Here is the sample output-

Related

Rank and Rank Increment/Decrement from selected week vs previous week

I have a table visual with branch, client and revenue information. The revenue is coming from a measure which is affected by the week selector slicer.
I need to show Rank for selected week and rank increment/decrement from selected week's rank vs previous week from selected week. In snapshot, Rank # and Rank are the requirements.
I tried to create rank with AllSelected function but it is always affected by week slicer and I cannot get previous week rank to compare and put Rank increment/decrement.
DAX I tried for Rank
This Week GP =
var _Total =
CALCULATE(
SUM(V_TopClients[revenue]),
DATESBETWEEN('Date'[date],[current_week_start_date], [current_week_end_date])
)
return _Total
Previous Week GP =
var _Total =
CALCULATE(
SUM(V_TopClients[revenue]),
DATESBETWEEN('Date'[date],[previous_week_start_date], [previous_week_end_date])
)
return _Total
Rank This Week =
RANKX(
ALLSELECTED(V_TopClients),
CALCULATE(SUM(V_TopClients[Revenue])
)
)
--Update: added dax for measures
Here I am able to get the rank for this week i.e. the week selected on slicer.
But unable to get rank for previous week.
I have v_TopClients that has weekly revenue information linking to dimCalendar, Date tables.
Here is another and probably the best option for you-
Step-1:
Create a new custom table based on your table "TopClient". The code is as below-
group_by_result_new =
VAR group_wise_revenue =
GROUPBY (
TopClients,
TopClients[CalendarWeekKey],
TopClients[clientID],
"gp_this_week", SUMX(CURRENTGROUP(), TopClients[GrossProfit])
)
RETURN
SELECTCOLUMNS (
group_wise_revenue,
"CalendarWeekKey", TopClients[CalendarWeekKey],
"clientID", TopClients[clientID],
"gp_this_week", [gp_this_week]
)
Step-2:
Create relation between table "DimCalendar" and "group_by_result_new" using the column "CalendarWeekKey"
Step-3:
Create a new column (remember it a column) as below-
gp_prev_week =
VAR client_id = group_by_result_new[clientID]
VAR calendar_key_this_week = group_by_result_new[CalendarWeekKey]
VAR end_date_this_week =
LOOKUPVALUE(
DimCalendar[WeekEndingDate],
DimCalendar[CalendarWeekKey], CONVERT(calendar_key_this_week,INTEGER)
)
VAR end_date_prev_week = CONVERT(end_date_this_week,DATETIME) - 7
VAR calendar_key_prev_week =
LOOKUPVALUE(
DimCalendar[CalendarWeekKey],
DimCalendar[WeekEndingDate] , end_date_prev_week
)
VAR gp_prev_week =
LOOKUPVALUE(
group_by_result_new[gp_this_week],
group_by_result_new[CalendarWeekKey],calendar_key_prev_week,
group_by_result_new[clientID], CONVERT(client_id,INTEGER)
)
RETURN gp_prev_week
Step-4:
Create a new column (remember it a column) for RANK this week as below-
rank_this_week =
RANKX (
FILTER (
group_by_result_new,
group_by_result_new[CalendarWeekKey] = EARLIER (group_by_result_new[CalendarWeekKey])
),
group_by_result_new[gp_this_week],
,
DESC
// ,
// DENSE
)
Step-5:
Create a new column (remember it a column) for RANK prev week as below-
rank_prev_week =
RANKX (
FILTER (
group_by_result_new,
group_by_result_new[CalendarWeekKey] = EARLIER (group_by_result_new[CalendarWeekKey])
),
group_by_result_new[gp_prev_week],
,
DESC
// ,
// DENSE
)
And that's all! This should also work same as my previous solution.
Cheers!!
In DAX, there are builtin function PREVIOUSMONTH, PREVIOUSQUARTER and PREVIOUSYEAR available. But as you are searching for weekly data comparison, you required your own date periods to calculate. I just can give you some idea as below-
First, crate 4 measure based on your slicer week/date selection.
Example:
current_week_end_date = SELECTEDVALUE(Dates[Date])
current_week_start_date = SELECTEDVALUE(Dates[Date]) - 7
previous_week_end_date = SELECTEDVALUE(Dates[Date]) - 8
previous_week_start_date = SELECTEDVALUE(Dates[Date]) - 15
Now, you need 2 separate measure to calculate this week and previous week total revenue. Example Measures are given below-
1.
this_week_revenue =
CALCULATE(
SUM(table[revenue]),
DATESBETWEEN(
'Dates'[Date],
[current_week_start_date],
[current_week_end_date]
)
)
2.
previous_week_revenue =
CALCULATE(
SUM(table[revenue]),
DATESBETWEEN(
'Dates'[Date],
[previous_week_start_date],
[previous_week_end_date]
)
)
Now you have both weekly value in your hand and you can compare measure "this_week_revenue " with measure "previous_week_revenue" to generate the directions indicators.
Hope this will help!
Below image is just for reference:

Power BI: Calculating average of a column's current row divided by the first

Please see the file I have shared in the following link:
https://drive.google.com/file/d/1q7FE85qHoB_OhAm4hlmFidh6WP3m-UnK/view?usp=sharing
I have a dataset with the value of various items on different dates. I first need to calculate ratio by dividing the value of an item on a particular date with the earliest value in the chosen date filters. I then need to show the per day ratio of all items (columns I & J) as the desired output.
I am able to get the "first value" in the filtered table by using the following formula:
first_cr =
VAR item = MAX('Table 1'[item])
VAR min_updated = CALCULATE(
MIN('Table 1'[last_updated]),
'Table 1'[item] = item,
ALLSELECTED('Table 1')
)
RETURN
CALCULATE(
AVERAGE('Table 1'[cr]),
'Table 1'[last_updated] = min_updated,
'Table 1'[item] = item,
ALLSELECTED('Table 1'[fk])
)
However, I am unable to figure out how to calculate the average of all the individual item ratios at just the date level. I guess I need to use AVERAGEX somehow, but not sure how. Perhaps my first_cr formula could use more refinement.
I'd appreciate any help or guidance in the right direction. Thanks for your time.
I was able to get the answer using the following formula. If anyone can refine it better, please do so, else I'll accept my answer after a few days.
ret =
var lastUpdated = MAX(Sheet1[Date])
var tbl = ALLSELECTED(Sheet1)
RETURN
CALCULATE(
AVERAGEX(
Sheet1,
var i = Sheet1[Item]
var minUpdated = CALCULATE(
MIN(Sheet1[Date]),
Sheet1[Item] = i,
tbl
)
var first_cr = CALCULATE(
AVERAGE(Sheet1[Return]),
Sheet1[Date] = minUpdated,
Sheet1[Item] = i,
tbl
)
RETURN Sheet1[Return] / first_cr
),
Sheet1[Date] = lastUpdated,
tbl
)

Sum row with next row value and grab next value in other column for the date selected

Let's say I have this data:
Earn Earn Cum.
13-Apr - -
14-Apr 48 48
15-Apr 257 305
16-Apr 518 823
17-Apr 489 1,312
18-Apr 837 2,149
19-Apr 1,005 3,154
20-Apr 1,021 4,175
21-Apr 1,463 5,638
22-Apr 2,630 8,268
23-Apr 2,993 11,261
24-Apr 3,354 14,615
25-Apr 4,332 18,947
26-Apr 4,885 23,832
27-Apr 4,514 28,346
28-Apr 4,356 32,702
29-Apr 4,824 37,526
30-Apr 7,082 44,608
1-May 6,091 50,699
2-May 1,407 52,106
When a date is selected in a dropdown slicer for example: 1-May I'd like to sum the rows 1-May with the next row 2-May for the column Earn and grab the next value 52,106 for the column Earn Cum.. The result should be:
1-May 7,498 52,106
Another example: if the date selected was 30-Apr the result must be:
30-Apr 13,173 50,699
I'm scratching my head trying to do this using a measure in Power BI.
I'll call your table "Data".
Create a measure:
Next Earn =
VAR Current_Date = MAX ( Data[Date] )
VAR Next_Date = Current_Date + 1
RETURN
CALCULATE (
SUM ( Data[Earn] ),
Data[Date] = Current_Date || Data[Date] = Next_Date
)
Create another measure:
Next Cum Earn =
VAR Current_Date = MAX ( Data[Date] )
VAR Next_Date = Current_Date + 1
RETURN
CALCULATE ( SUM ( Data[Earn Cum] ), Data[Date] = Next_Date )
Result:
Note: the code assumes that your dates are sequential (no gaps). If they have gaps, things are a bit more complex.
It will help to have access to date intelligence, so create a date table if you don't have one already. The following dax sets up a small table that just barely covers the sample data in your example.
Date = CALENDAR(Date(2019,4,10),Date(2019,5,5))
Create a relationship between the Dates in your table and the new Date dimension. Add a slicer to your visuals using the Date dimension.
We can use IF and ISFILTERED to check if filtering is being done. If we aren't filtering on Date then we get the normal table behavior. If we are, we'll see our modified result.
Earn _ Alt =
var tomorrow = CALCULATE (
SUM(Table1[Earn]),
dateadd('Table1'[Date], 1, DAY)
)
return Sum(Table1[Earn]) + IF(ISFILTERED`('Date'[Date]),tomorrow,0)`
and
Earn Cum. _ Alt = IF(ISFILTERED('Date'[Date]),
CALCULATE (
SUM(Table1[Earn Cum.]),
dateadd('Table1'[Date], 1, DAY)
),
SUM(Table1[Earn Cum.]))
Results:
-Unfiltered-
-Filtered-

DAX to calculate cumulative sum column (year to date) for all individual products

I have 3 columns in a table: Date, Product, and Volume. Based on this is I want to calculate the cumulative sum (or YTD) column for each of the products.
I know that to calculate cumulative total we use:
YTD_Actual = CALCULATE(Sum(Combined[Actual Volume]),FILTER(Combined,Combined[Date] <= EARLIER(Combined[Date])))
But I want this to additionally filter individual products and do this calculation for that product.
The expected column is shown in the picture below:
As far as you manage to get the Month column in date format, the following works:
YTD =
VAR currProduct = Table1[Product]
VAR currMonth = Table1[Month]
VAR ytdTotal =
CALCULATE(
SUM(Table1[Volume]),
FILTER(
ALL(Table1),
Table1[Month] <= currMonth &&
Table1[Product] = currProduct
)
)
RETURN IF(NOT(ISBLANK(ytdTotal)), ytdTotal,0)
Result:

Calculate sales of products that sold this year but not the year before

I want to calculate the sum of NetSales for the products that had sales this year (2019) but NOT sales last year (2018).
This is what I'm trying (and a million variations more similar to this):
NetSales CY Not LY =
VAR ThisYear= 2019
VAR YearBefore= 2018
VAR TabelaThisYear = SUMMARIZE(FILTER(SUMMARIZE('Facts';Facts[ArticleNo];Facts[InvoiceDate];Facts[NetSalesCurr]);YEAR('Facts'[InvoiceDate])=ThisYear && Facts[NetSalesCurr]>0);Facts[ArticleNo])
VAR TabelaYearBefore = SUMMARIZE(FILTER(SUMMARIZE('Facts';Facts[ArticleNo];Facts[InvoiceDate];Facts[NetSalesCurr]);YEAR('Facts'[InvoiceDate])=YearBefore && Facts[NetSalesCurr]>0);Facts[ArticleNo])
VAR ProdutosOnlyThisYear = EXCEPT(TabelaThisYear;TabelaYearBefore)
RETURN
CALCULATE(SUM(Facts[NetSalesCurr]);ProdutosOnlyThisYear)
I would use the following approach, where we don't hardcode the years:
NetSales CY Not LY=
CALCULATE(
SUM(Facts[NetSalesCurr]),
FILTER(
VALUES(Facts[ArticleNo]),
CALCULATE(
COUNTROWS(Facts),
PREVIOUSYEAR(Calendar[Date])
) = 0
)
)
Let's break it down:
The inner FILTER function iterates through VALUES(Facts[ArticleNo]) - that is all ArticleNo's that have sales in the currently selected period.
For every one of those ArticleNo's, we calculate the number of rows on the fact table for the previous year. CALCULATE(COUNTROWS(Facts), PREVIOUSYEAR(Facts[InvoiceDate]))
We keep only those ArticleNo's where this number is = 0.
The list of ArticleNo's returned from FILTER is then used in the outer CALCULATE statement, so that we only get the sum of NetSales for articles that did not have any sales in the previous year.
For more information, please see the New and returning customers-pattern.
you can use the dax formula like at the below , if you have a calendar table ;
this year = CALCULATE ( SUM[measure];DATEADD(CALENDAR[DATE]),0,YEAR)
last year = CALCULATE ( SUM[measure];DATEADD(CALENDAR[DATE]),-1,YEAR)