Calculating cumulative values - powerbi

I am trying to calculate cumulative values. I tried the formulas below, none of them worked.
Cumulative_Forecast = CALCULATE(sum(TEAMS_Forecast_LineItems[ForeCast_Value]),
filter(ALLEXCEPT(TEAMS_Forecast_LineItems,TEAMS_Forecast_LineItems[ForeCast_Year]),
TEAMS_Forecast_LineItems[MonthNumber]<=EARLIER(TEAMS_Forecast_LineItems[MonthNumber])))
Cumulative_Forecast2 = VAR RowDate = TEAMS_Forecast_LineItems[Forecast_Date]
return CALCULATE(sum(TEAMS_Forecast_LineItems[ForeCast_Value]),
FILTER(TEAMS_Forecast_LineItems, TEAMS_Forecast_LineItems[Forecast_Date]
<=RowDate && YEAR ( TEAMS_Forecast_LineItems[Forecast_Date] ) = YEAR ( RowDate )))
Cumulative_Forecast3 = TOTALYTD(sum(TEAMS_Forecast_LineItems[ForeCast_Value]),
'Calendar'[Date])
Cumulative_Forecast4 = CALCULATE(sum(TEAMS_Forecast_LineItems[ForeCast_Value]),
filter(ALL(DimDate[Date]), DimDate[Date] <= Max(DimDate[Date])) )
Here are some examples of records:
ForeCast_Value Month MonthNumber ForeCast_BU ForeCast_Year ForeCast_ID
71100 Sep 9 Business1 2018 10648
71100 Oct 10 Business1 2018 10648
81000 Sep 9 Business1 2018 10649
71200 Sep 9 Business2 2018 10700
80500 Sep 9 Business2 2017 10500
80600 Oct 10 Business2 2017 10500
81100 Sep 9 Business2 2018 10650
I have a line chart; Month on the Axis, BU on the legend and value on values.
When slicer year is all selected
Business 1= 152100 and Business 2 =232800 on September
Business 1= 71100 and Business 2 =80600 on October
When slicer year is 2017 selected
Business 2=80600 on Sep
Business 2=80500 on Oct
I want to create a new line chart for cumulative values. Desired values are:
When slicer year is all selected
Business 1= 152100 and Business 2 =232800 on September
Business 1= 223200 and Business 2 =313400 on October
When slicer year=2017 selected
Business 2=80600 on Sep
Business 2=161100 on Oct

The below DAX formulas can help you to get your expected results:
Cumilative_Forecast =
CALCULATE (
SUM ( TEAMS_Forecast_LineItems[ForeCast_Value] ),
FILTER(ALL(TEAMS_Forecast_LineItems),TEAMS_Forecast_LineItems[ForeCast_BU] = max ( TEAMS_Forecast_LineItems[ForeCast_BU] )) , FILTER(ALL(TEAMS_Forecast_LineItems), TEAMS_Forecast_LineItems[MonthNumber] = max ( TEAMS_Forecast_LineItems[MonthNumber] )),FILTER (
ALL ( TEAMS_Forecast_LineItems ),
TEAMS_Forecast_LineItems[ForeCast_Year] <= max( TEAMS_Forecast_LineItems[ForeCast_Year] ))
)
The above formula gives result of
" When slicer year is all selected
Business 1= 152100 and Business 2 =232800 on September
Business 1= 71100 and Business 2 =80600 on October "
Cumulative_Value =
CALCULATE (
SUM ( TEAMS_Forecast_LineItems[ForeCast_Value] ),
FILTER(ALL(TEAMS_Forecast_LineItems),TEAMS_Forecast_LineItems[ForeCast_BU] = max ( TEAMS_Forecast_LineItems[ForeCast_BU] )) , FILTER (
ALL ( TEAMS_Forecast_LineItems ),
TEAMS_Forecast_LineItems[MonthNumber] <= max( TEAMS_Forecast_LineItems[MonthNumber] ))
)
The above formula gives result of
"I want to create a new line chart for cumulative values. Desired values are:
When slicer year is all selected
Business 1= 152100 and Business 2 =232800 on September
Business 1= 223200 and Business 2 =313400 on October "
Please let me know does this reached your expected result or not.

Related

PowerBI: Count email subjects by month - assign subject to the month it first appeared

I have a dataset similar to the following:
Email Subject
Date Received
Month Year received
Red
1 Jan 21
Jan 21
Blue
1 Jan 21
Jan 21
Green
3 Feb 21
Feb 21
Red
5 Feb 21
Feb 21
What I am looking for is a count of distinct email subjects by month however if a subject appears in the previous month then don't count it.
The ideal outcome using the data above would be a:
Total count (No month breakdown) = 3 ( 3 unique subjects - Red, Blue & Green)
Count of subjects for Jan = 2 (Red and Blue)
Count of subjects for Feb = 1 (Only green because Red appeared in the previous month)
This is so I can count how many email threads were received each month without duplicating a thread that spanned over multiple months.
Any help would be greatly appreciated - been stuck for ages!
Assuming you have no date table (if you do, you can adjust the code accordingly), here you go.
Measure =
VAR month = MONTH(MAX('Table'[Date Received]))
VAR year = YEAR(MAX('Table'[Date Received]))
VAR ddate = MAX('Table'[Date Received])
VAR currentTable =
CALCULATETABLE(VALUES('Table'[Email Subject]),
FILTER(ALL('Table'),
MONTH('Table'[Date Received]) = month && YEAR('Table'[Date Received]) = year)
)
VAR excludeTable =
CALCULATETABLE(VALUES('Table'[Email Subject]),
FILTER(ALL('Table'),
'Table'[Date Received] < EOMONTH(ddate,-1))
)
VAR result = EXCEPT( currentTable,excludeTable)
RETURN IF(HASONEVALUE('Table'[Email Subject]),COUNTROWS(result),DISTINCTCOUNT('Table'[Email Subject]))
You can use a measure like this
Measure =
VAR mxp =
MAX ( 'Fact'[Month Year received] )
VAR _cp =
CALCULATETABLE (
VALUES ( 'Fact'[Email Subject] ),
'Fact'[Month Year received] = mxp
)
VAR _ytd =
CALCULATETABLE (
VALUES ( 'Fact'[Email Subject] ),
'Fact'[Month Year received] < mxp
)
VAR _join =
COUNTX ( EXCEPT ( _cp, _ytd ), [Email Subject] )
RETURN
IF (
HASONEVALUE ( 'Fact'[Month Year received] ),
_join,
COUNTROWS ( VALUES ( 'Fact'[Email Subject] ) )
)
mxp gets what is currently visible to DAX
'Fact'[Month Year received] = mxp gets only the current month's unique email subjects.
'Fact'[Month Year received] < mxp gets anything up-to immediately preceding to whatever is currently visible
_join does a left-anti join that brings whatever unique is visible in the current month.
subtotal are handled little differently through IF(HASONEVALUE('Fact'[Month Year received]),_join,COUNTROWS(VALUES('Fact'[Email Subject])))

Calculate max value from Mon-Sun for every week

I have a table where I have to find the maximum weekly quantity for the data based on the date column in the past few years. In this example, say 06/07 is Monday and 06/12 is Sunday. Then in that week, the quantity 12 is the MAX value.
Quantity
Date
Time
1
2022-06-07
04:39:57.090
2
2022-06-07
04:39:58.850
3
2022-06-08
04:37:35.900
4
2022-06-08
04:37:37.247
5
2022-06-09
04:37:56.833
6
2022-06-09
04:37:58.190
7
2022-06-10
04:37:34.757
8
2022-06-10
04:37:36.103
9
2022-06-11
04:38:18.733
10
2022-06-11
04:38:20.100
11
2022-06-12
04:38:30.377
12
2022-06-12
04:38:31.833
13
2022-06-13
04:38:30.377
14
2022-06-14
04:38:31.833
I am trying to write a DAX script for this, but failing to do it because I am filtering on MAX date instead of value. Any help on how to modify this?
CALCULATE(
MAX( 'stock_xml'[Quantity] ),
FILTER( 'stock_xml',
'stock_xml'[Date] = MAX( 'stock_xml'[Date] )
)
)
RETURN
Stock
Can you please try this below Measure script-
weekly_max =
CALCULATE(
MAX('your_table_name'[Quantity]),
FILTER(
ALL('your_table_name'),
WEEKNUM('your_table_name'[Date],1) = WEEKNUM(MAX('your_table_name'[Date]),1)
)
)
Sample output-

Conditional Sum in Power BI Matrix

I have a Power BI matrix with 3 columns: 2019, 2020, and 2021. Each row is an item number, and each value is a total quantity produced in the corresponding year column.
I am trying to find a way to display the sum of ONLY items that were blank in 2019 and 2020, BUT have a total quantity in the 2021 column. Is there a way to conditionally format this?
Basically, another way of saying the logic is: total sum of all items where 2019 AND 2020 are blank, AND 2021 is not blank.
Try this solution if it helps then mark it as the answer.
Create a calculated column with the following DAX:
Column =
IF (
(
Table[Year] = 2019 || Table[Year] = 2020 ),
IF ( ISBLANK ( Table[Item] ), ( Table[Quant] ) ),
IF ( Table[Year] = 2021, IF ( ISNUMBER ( Table[Item] ), ( Table[Quant] ) ) )
)
Input I used:-
Output where 2019 and 2020 Items are blank and 2021 where Items are not blank:-
You can create the following measure:
Measure =
IF (
CALCULATE(
SUM(Table[Quantity]),
Table[Year] => 2019,
Table[Year] <= 2020,
) > 0,
0,
CALCULATE(
SUM(Table[Quantity]),
Table[Year] = 2021
)
)
And in the table you should filter that visual so it only shows values where this Measure is > 0. However, if your intention is to have this dynamically (i.e. so this still makes sense next year with years 2020, 2021 and 2022), I would recommend this:
Measure =
IF (
CALCULATE(
SUM(Table[Quantity]),
Table[Year] => YEAR(TODAY()) - 2,
Table[Year] <= YEAR(TODAY()) - 1,
) > 0,
0,
CALCULATE(
SUM(Table[Quantity]),
Table[Year] = YEAR(TODAY())
)
)

PowerBI join table and calculation

I have two tables:
Table 1
Year Name Score
2010 A 1000
2011 A 1200
2012 A 1200
2013 A 1300
2010 B 1100
2011 B 1500
2012 B 1300
2013 B 1600
Table 2
Year Factor
2010 3.57
2011 4.21
2012 6.11
2013 2.15
I would like to do this: match two tables, as long as Year in Table 1 match Year in Table 2, divide the score by the factor, e.g. for A in 2010, 1000/3.57; for B in 2010,1100/3.57
How to revise the following code?
adjusted score =
VAR
filter (all('Table 1'[Year]), 'Table 1'[Year]='Table 2'[Year])
RETURN
DIVIDE ('Table 1' [Score], 'Table 2'[Factor')
Thank you very much!
If you are looking for a measure, you can try this below code-
adjusted score =
VAR current_row_year = MIN('Table 1'[Year])
VAR current_row_Score = MIN('Table 1'[Score])
VAR factor =
CALCULATE(
MAX('Table 2'[Factor'),
filter (
all('Table 2'),
'Table 2'[Year]= current_row_year
)
)
RETURN DIVIDE (current_row_Score, factor)
Code for a Custom Column is as below-
adjusted score =
VAR current_row_year = 'Table 1'[Year]
VAR current_row_Score = 'Table 1'[Score]
VAR factor =
CALCULATE(
MAX('Table 2'[Factor'),
filter (
all('Table 2'),
'Table 2'[Year]= current_row_year
)
)
RETURN DIVIDE (current_row_Score, factor)
You can also do the same using Transformation using Power query. In that case you have to just merge your table 1 and 2 using Year and then after Expanding the table, you will have Factor in each row for the Year. You can then just add a new custom column with - Score/Factor

Calculate Number of Working Days based on a Month and Year Column - DAX

I have a column like below for which I would want to extract the number of working days (excluding just weekends- Saturday and Sunday, holidays needs not be addressed).
As of now I just want:
Required_Column = Total No of Days in that month - No of weekends in that month
Month_Year
01-2018
02-2018
03-2018
...
...
01-2019
02-2019
I am a newbie in Power query and DAX , i tried looking up various methods using DAX however, could not find any relevant lead.
Expected Output:
Month_Year Required_Column
01-2018 23 (31-8)
02-2018 20 (28-8)
03-2018 22 (31-9)
... ...
... ...
01-2019 23 (31-8)
02-2019 20 (28-8)
Appreciate your help on this.
Although a Calendar table based approach is recommended as in the comment by RADO and Strawberryshrub, it is also possible to do this with DAX calculated column.
In the example below, I'm assuming MonthYear column contains the first day of each month.
WorkingDays =
VAR Year = YEAR( [MonthYear] )
VAR Month = MONTH( [MonthYear] )
VAR DatesInMonth = GENERATESERIES( [MonthYear], DATE( Year, Month + 1, 1 ) - 1, 1 )
RETURN SUMX(
DatesInMonth,
IF( WEEKDAY( [Value] ) IN { 1, 7 }, 0, 1 )
)