How to calculate Cumulative Sum in Power BI - powerbi

I know how to do cumulative sum if dataset has Dates, But I am struggling to do same if I do not have dates in my dataset.
Below is the data, I want CUM Sales

I've selected New quick measure -> Totals -> Running total and creates this:
sales running total in part =
CALCULATE(
SUM('Query1'[sales]);
FILTER(
ALLSELECTED('Query1'[part]);
ISONORAFTER('Query1'[part]; MAX('Query1'[part]); DESC)
)
)
Returns:

You can also create own measure to calculate cumulative sum.
Select New Measure from the ribbon and write the following expression
Cumm Sales =
VAR Current_Part = MAX(Test[Part])
RETURN
CALCULATE(
SUM(Test[Sales]),
Test[Part]<=Current_Part,
ALL(Test[Part])
)
Output:

Related

How to calculate average percentage in PowerBI?

Hi everyone,
I'm still new to PowerBI, right now I have a set of data in PowerBI as shown in the screenshot above. I have a Measure to calculate the % of OK:
total_student = COUNT(StudentAns[Name])
ok_% =
VAR OK_COUNT = COUNTROWS(
FILTER(
StudentAns,
StudentAns[Answer] = "OK"
)
)
RETURN (OK_COUNT/StudentAns[total_student])
I created a Matrix to show the % of OK for each month as shown in the screenshot below:
What I want to find is the average percentage for all the months. So the final output answer should be 89.05%, which is the average of 85.95%, 91.4%, 89.27% and 89.58%.
The reason I want to get the average percentage of OK across all the months is because I want to use the output as a Target Goals for KPI visualization.
Any help or advise will be greatly appreciated!
You can add one more measure to the matrix as follows:
ok_2 % =
IF(
HASONEVALUE( 'StudentAns'[Month] ),
[ok_%],
AVERAGEX(
VALUES( StudentAns[Month] ),
[ok_%]
)
)
It calculates your original measure for each month, but for the Totals it returns the average of results of your measure.
HASONEVALUE returns True if there is only one distinct value in the filtered context; VALUES - creates a list of unique values; AVERAGEX - calculates the average of a set of expressions evaluated in each row.

Counting distinct IDs for each date in Power BI

I have a dataset and I want to create a column(not measure) to calculate the count of customers in each month. I don't know how I can count each customer once a month in Power BI.
I wrote this code but it counts the number of frequent customers more than once a month.
myCol = CALCULATE( DISTINCTCOUNT('table'[user_id] ) , 'table'[order_date] )
For example, it's my data:
The true result should be:
but my code returns this result:
How should I write the code for this calculating column to get a true result?
Since you are trying to calculate per month, you need a "year_month" column.
Then:
count_of_customer =
CALCULATE(
DISTINCTCOUNT('table'[user_id]),
ALLEXCEPT('table', 'table'[year_month])
)
Result:
Edit:
You don't need a calculated column, you need a measure:
count_of_customer =
COUNTROWS (
SUMMARIZE ( 'table', 'table'[year_month], 'table'[user_id])
)

Power BI DAX measure calculation

Step1:
I have created a calculated table containing Level, Location, L2code from level sales data. I need to create a report that will count rows based on the level1 group.
Note that there are more levels in the table. This is only an example of one of the levels.
How can I count the rows for each level1?
Step2:
I need to create all combinations of counts based on location and l2 code and count the numbers.
like in example 2 location and 8 distinct l2code so it should be 2*8 =16 total possible rows.
How can I achieve this using the DAX measure?
Source data file
Output report
first one is a simple measure as below-
DistinctRowCount = Count(your_table_name[Level1])
Second one is bit tricky and you can try this below measure-
CrossCount =
var distinct_location =
calculate(
distinctcount(your_table_name[Location]),
allexcept(
your_table_name,
your_table_name[Level1],
your_table_name[Location]
)
)
var distinct_l2code =
calculate(
distinctcount(your_table_name[l2code]),
allexcept(
your_table_name,
your_table_name[Level1],
your_table_name[Location],
your_table_name[l2code]
)
)
return distinct_location * distinct_l2code
Sample output-

PowerBI DAX: How to calculate a running total for both date and another filter

I need to calculate a running total of a measure by date and by source. Below is a screenshot how my table looks like now.
Below is the measure code.
CashPosition_Revenue Running Total = CALCULATE([CashPosition_Revenue],FILTER(VALUES(FilterKeys_Date[Date]), FilterKeys_Date[Date] <= MAX(FilterKeys_Date[Date])))
I need last column to be calculated as running total.
Using Values limits your filter context to the current date only, try using All to remove that filter:
CashPosition_Revenue Running Total =
VAR CurrentDate = MAX(FilterKeys_Date[Date])
RETURN
CALCULATE(
SUM(FilterKeys_Date[CashPosition_Revenue]),
FILTER(
ALL('FilterKeys_Date'),
FilterKeys_Date[Date] <= CurrentDate
)
)

Calculate Average Number of Days Between Multiple Dates with dax in power bi

Let's say I have the following data frame. I want to calculate the average number of days between all the activities for a particular account using Dax in power bi
Let say I have this:
I want a desired result like this
How do I achieve this using DAX in power BI
Assuming you have the data in a table as in your picture, create a calculated column like this:
AvgerageDaysInbetween =
var thisCustomer = [Customer]
var temp =
SUMX(
FILTER(
SUMMARIZE(
'Table';
[Customer];
"avg";
DIVIDE(
DATEDIFF(MIN('Table'[DateOfOrder]); MAX('Table'[DateOfOrder]); DAY);
DISTINCTCOUNT('Table'[DateOfOrder])-1;
0
)
);
[Customer] = thisCustomer
);
[avg]
)
return
temp
Resulting table: