Calculating average from measures - powerbi

This one should be quite easy, but I can't find a way to solve it...
A very simple setup: Three tables: products, sales and budgets.
Budget has four columns from source: ProductID,StartOfperiod,BudgetNumber.
Then I've added a calculated column, "Results" which calculate saleresults by product, month and year:
Results =
CALCULATE (
SUM ( sales[Numbers] );
FILTER ( sales; sales[ProductID] = Budget[ProductID] );
FILTER ( sales; MONTH ( sales[DateofSale] ) = MONTH ( Budget[StartOfperiod] ) );
FILTER ( sales; YEAR ( sales[DateofSale] ) = YEAR ( Budget[StartOfperiod] ) )
)
Works fine.
Then I added a measure for percentage of sales/budget:
SalesPercentage =
DIVIDE ( SUM ( Budget[Results] ); SUM ( Budget[BudgetNumber] ); 0 )
This also shows up fine.
I calculate for four products only, so when put in a matrix this gives a nice table showing sales results for these products.
But then I want to calculate the arithmetic average of SalesPercentage and put that value on i.e. a card. But how can I do that? All the necessary data are available in the budget table, but I can't find a way to just calculate the average of the SalesPercentage column, which is a measure. Anyone who knows how to?
Regards,
John Martin

Related

Cumulative total percentage in Power BI

I have a summary table in Power BI which shows how many days it takes for leads to convert to a sale. It has 2 columns, sum_convert (the amount of days in between lead creation date and converted date) and count_lead (the count of leads that have taken that amount of days to convert), both are numeric values. Here is an example of the data:
What I want, is a column next to count_lead that shows the running percentage total in the specific ascending order of sum_convert. Currently I've created a measure called lead_count which is the sum of count_lead. Then I've attempted to create the cumulative total with the following measure:
Cum_Lead = calculate([lead_count], FILTER(ALL(Convert_Count_Summary[Sum_Convert]), SUM(Convert_Count_Summary[count_lead]) <= [lead_count]))
This creates a cumulative total, but not in the specific sum_convert order, it's in the order of largest volume for count_lead. Any idea what I need to change so that it's in the order of sum_convert?
You could do this in Power Query using M:
= Table.AddColumn(#"Previous Step", "Cumulative_Count_PQ", each List.Sum(List.FirstN(#"Previous Step"[count_lead],_[sum_convert]+1)), type number)
Or as a calculated column using DAX:
Cumulative Count DAX =
CALCULATE (
SUM ( Convert_Count_Summary[count_lead] ),
ALL ( Convert_Count_Summary ),
Convert_Count_Summary[sum_convert] <= EARLIER ( Convert_Count_Summary[sum_convert] )
)
Edit:
Cumulative percentages in Power Query:
= Table.AddColumn(#"Previous Step", "Cumulative_Count_Percent_PQ", each List.Sum(List.FirstN(#"Previous Step"[count_lead],_[sum_convert]+1)) / List.Sum(#"Previous Step"[count_lead]), Percentage.Type)
Cumulative percentages calculated column in DAX:
Cumulative Count % DAX =
VAR _Numerator =
CALCULATE (
SUM ( Convert_Count_Summary[count_lead] ),
ALL ( Convert_Count_Summary ),
Convert_Count_Summary[sum_convert] <= EARLIER ( Convert_Count_Summary[sum_convert] )
)
VAR _Divisor =
SUM ( Convert_Count_Summary[count_lead] )
RETURN
DIVIDE (
_Numerator,
_Divisor
)

How to combine Max and Average in Power BI with Filters

I want to create a measure (Average Late) in powerBI that calculates the Maximum of Late per Ordernumber, sum them up & divide the by the number of orders.
enter image description here
I also want this measure to be dynamic and only calculate whats displayed with filters.
enter image description here
I have tried with functions such as CALCULATE, MAX, DISTINCT & SUM.
Try this:
Measure =
VAR distinct_ordernumber =
DISTINCTCOUNT ( 'Table'[OrderNumber] )
VAR sum_max_late_per_ordernumber =
SUMX (
SUMMARIZE ( 'Table'; 'Table'[OrderNumber]; "max_late"; MAX ( 'Table'[Late] ) );
[max_late]
)
RETURN
FORMAT ( sum_max_late_per_ordernumber / distinct_ordernumber; "##.00" )

Is there a way to create measure using variable to return last month Base size in power BI or DAX

I'm creating a report in Power BI, and want to return the last month Size.
I have a table with 4 columns named as Name, Size, Connections, Disconnections. The values on these columns are for the last 12 months. For example, Name column has A, B, C; Size column has 3608445, 2839945,874434; Connections column has 66875,85632,19237 and Disconnections column has 52658,61529 and 15832 values. These values are for the last 12 months. See screenshot below.
The code I used to created the expected table is
last_month_size =
VAR current_month =
MONTH ( TODAY () )
RETURN
CALCULATE (
[Size],
FILTER (
'Monthly Calendar_Lookup',
MONTH ( 'Monthly Calendar_Lookup'[Dates] ) = current_month - 1
)
)
I want to create a measure that will return last month Size column but the Connections and Disconnections remains the same. For example, the Size value changes while the connections and disconnections values remains the last 12 month values.
I find it difficult because the columns are on the same table.
I have researched about the question I have posted and I have found a solution.
This solution to the problem is creating measures and not using variables.
First, I created a measure called Total Size
Total Size = Sum ( Tablename [Size] )
Then, created another measure called prev_month size using DATEADD function with number_of_intervals as 0
prev_month size = CALCULATE ( [Total Size], DATEADD ('Monthly Calendar_Lookup'[Dates], 0, MONTH ) )
Next, I created measures of total connections and total disconnections
Total Connections = Sum ( Tablename [Connections] )
Total Disconnections = Sum ( Tablename [Disconnections] )
Also, I created two measures of rolling 12 months Connections and Disconnections each.
Rolling_Connections_12_months =
CALCULATE ( SUMX ('Tablename', [Total Connections] ),
DATESINPERIOD ('Date'[Month], LASTDATE ( 'Date'[Month] ), -12, MONTH ) )
Rolling_Disconnections_12_months =
CALCULATE ( SUMX ('Tablename', [Total Disconnections] ),
DATESINPERIOD ('Date'[Month], LASTDATE ( 'Date'[Month] ), -12, MONTH ) )
Drag the Name, prev_month size, Rolling_Connections_12_months, and Rolling_Disconnections_12_months on the canvas as a table visualization.
Then finally, I drag a relative Date slicer and set it as Last 1 Month.
This produces the expected results

Total Working Days Calculation in PowerBi

I have the that has some data related to employee start date and end date for specific task
How to calculate "Total Working days" using the above data in PowerBi
The expected ouput would be number of days this employee worked (according to date )
so for example
12/2/2018 12/3/2018 = 1 working Day
but if same record repeated this would remain one working day
Output from Formula
Screenshot of PBX File
You can calculate the working day in various ways, I'll give you two options (both are calculated columns):
WorkingDays1 = Tasks[EndTime] - Tasks[StartTime]
(Assuming it's a date column type), you can add .DATE to be sure behind the column)
OR
WorkingDays2 = DATEDIFF(Tasks[StartTime];Tasks[EndTime];DAY)
If you want to correct the amount, for the same day, you can use an IF statement:
WorkingDays3 =
IF (
Tasks[StartTime] = Tasks[EndTime];
1;
DATEDIFF ( Tasks[StartTime]; Tasks[EndTime]; DAY )
)
If you wish to SUM the days, in a measure, you can SUM one of the generated columns above, OR you can create a measure which doesn't need such column: It iterates over the rows, and caculates the correct amount:
SUM WorkingDay =
SUMX (
Tasks;
IF (
Tasks[StartTime] = Tasks[EndTime];
1;
DATEDIFF ( Tasks[StartTime]; Tasks[EndTime]; DAY )
)
)
Result:
Note: These calculations do not take the weekends, or holidays into account.
Edit:
If you only want a unique list of dates, with the working days, you can use something like this. It itarates over the start dates:
SUM WorkingDay2 =
SUMX (
VALUES ( Tasks[StartTime] );
IF (
DATEDIFF ( Tasks[StartTime]; CALCULATE ( MAX ( Tasks[EndTime] ) ); DAY ) = 0;
1;
DATEDIFF ( Tasks[StartTime]; CALCULATE ( MAX ( Tasks[EndTime] ) ); DAY )
)
)
Edit 2:
If you want to create an unique list per day, you'll have to get rid of the time part in the datetime column. There are several ways to do this, but one option is to create a new column:
StartDate = Tasks[StartTime].[Date]
Then, you can adjust the measure by iteration over the dates, instead of the datetimes:
SUM WorkingDay3 =
SUMX (
VALUES ( Tasks[StartDate] );
IF (
DATEDIFF ( Tasks[StartDate]; CALCULATE ( MAX ( Tasks[EndTime] ) ); DAY ) = 0;
1;
DATEDIFF ( Tasks[StartDate]; CALCULATE ( MAX ( Tasks[EndTime] ) ); DAY )
)
)

Cumulative number of entries in dates range

I'm trying to get cumulative number of unique IDs in given timeframe.
My DAX look like this:
Cumulative = CALCULATE(SUM(Data[ID]));DATESBETWEEN(Data[ack_date];DATE(YEAR(NOW());4;1);DATE(YEAR(NOW());11;30)))
There is similar measure for Year-1: [YEAR(NOW())-1]
What I want to achieve is area chart showing growing number od IDs in time comparing same periods this and previous year. When I give those measures as Values for chart and "ack_date" as its Axis what I get is values comparison month by month but not cumulative, just value for certain month.
Try this code. Adjust for year -1.
=
CALCULATE (
DISTINCTCOUNT ( Data[ID] ),
FILTER (
ALL ( Data ),
AND (
Data[Ack_date] <= MAX ( Data[Ack_date] ),
AND (
Data[Ack_date] <= DATE ( YEAR ( NOW () ), 11, 30 ),
Data[Ack_date] >= DATE ( YEAR ( NOW () ), 4, 1 )
)
)
)
)