Dax Rolling Average with multiple records per day - statistics

Take a simple table
SalesTime
Product
UnitsSold
There is one row per sale. So there are multiple rows per day. I need a chart that will show the average units sold per sale over 7 days and average units sold per day over 7 days.
The examples that I found all used DATESBETWEEN or DATESINPERIOD and those throw an error if the table has multiple records per date.

I will name this table Sales and assume that Sales[SalesTime] is a date type rather than a datetime type. If not, create a new calculated column
Sales[SalesDate] = Sales[SalesTime].[Date]
and work with that instead.
Your rolling average units per sale can be calculated something like this:
AvgUnitsPerSale =
VAR CurrDay = MIN(Sales[SalesTime])
RETURN CALCULATE(
AVERAGE(Sales[UnitsSold]),
DATESBETWEEN(Sales[SalesTime], CurrDay-7, CurrDay))
You can get an average count of sales per day by using COUNT instead of AVERAGE. To get the average units sold per day, multiply the average count of sales and the average units per sale.

Related

Divide Weekly Budget by Workingdays

I need to divide the weekly budget by the number of working days per week to get a daily target.
The budget table has a structure like:
Area - YearWeek - Budget
The calendar table contains the column "IsWorkingday" with the information regarding the factor (1 for working day)
The following measure is not working:
Budget Phasing = DIVIDE([Budget], SUM('Calendar'[IsWorkingDay]))
[Budget] is a measure that contains the weekly budget.
As you can see in the picture below I get a right value on weekly level.
But I need the same value on a daily level.
daily target = Budget / SUM Workingdays
For example, for 25.05.2020 der should be 56.000 in der Budget Phasing
How can this be achieved?
Joshua, I think you are working with two tables - Budget and Calendar. Both tables are linked in my Data Model by [Week] column (which is a week number).
Try this measure, maybe this will help:
Budget Phasing =
var WeekNo = SELECTEDVALUE(tbl_Calendar[Week])
var WorkingDays = CALCULATE(
SUM(tbl_Calendar[IsWorkingDay]),
FILTER(ALL(tbl_Calendar), tbl_Calendar[Week] = WeekNo))
return
DIVIDE([Budget], WorkingDays)
The result seems to match your requirements:

DAX. Problem with subtotals and grand totals

hope you are doing well and can help solve this puzzle in DAX for PowerBI and PowerPivot.
I'm having troubles with my measure in the subtotals and grand totals. My scene is the following:
I have 3 tables (I share a link below with a test file so you can see it and work there :robothappy:):
1) "Data" (where every register is a sold ticket from a bus company);
2) "Km" (where I have every possible track that the bus can do with their respective kilometer). Related to "Data";
3) and a "Calendar". Related to "Data".
In "Data" I have all the tickets sold from a period with their price, the track that the passenger bought and the departure time of that track.
Each track can have more than 1 departure time (we can call it a service) but only have a specific lenght in kilometers (their kilometers are specified in the "Km" table). 
Basically what I need is to calculate the revenue per kilometer for each service in a period (year, month, day).
The calculation should be, basically:
Sum of [Price] (each ticket sold in the period) / Sum of [Km] (of the period considerating the services with their respective kilometers)
I managed to calculate it for the day granularity with the following logic and measures:
Revenue = SUM(Data[Price])
Unique dates = DISTINCTCOUNT(Data[Date])
Revenue/Km = DIVIDE([Revenue]; SUM(Km[Km])*[Unique dates]; 0)
I created [Unique dates] to calculate it because I tried to managed the subtotals of track granularity taking into account that you can have more than 1 day with services within the period. For example:
For "Track 1" we have registered:
1 service on monday (lunes) at 5:00am.
Revenue = $1.140.
Km = 115.
Tickets = 6.
Revenue/Km = 1.140/115 = 9,91.
1 service on tuesday (martes) at 5:00am.
Revenue = $67.
Km = 115.
Tickets = 2.
Revenue/Km = 67/115 = 0,58.
"Subtotal Track 1" should be:
Revenue = 1.140 + 67 = 1.207.
Km = 115 + 115 = 230.
Tickets = 6 + 2 = 8.
Revenue/Km = 1.207/230 = 5,25.
So at that instance someone can think my formula worked, but the problem you can see it when I have more than 1 service per day, for example for Track 3. And also this impact in the grand total of march (marzo).
I understand that the problem is to calculate the correct kilometers for each track in each period. If you check the column "Sum[Km]" is also wrong.
Here is a table (excel file to download - tab "Goal") with the values that should appear: 
[goal] https://drive.google.com/file/d/1PMrc-IUnTz0354Ko6q3ZvkxEcnns1RFM/view?usp=sharing
[pbix sample file] https://drive.google.com/file/d/14NBM9a_Frib55fvL-2ybVMhxGXN5Vkf-/view?usp=sharing
Hope you can understand my problem. If you need more details please let me know.
Thank you very much in advance!!!
Andy.-
Delete "Sum of Km" - you should always write DAX measures instead.
Create a new measure for the km traveled:
Total Km =
SUMX (
SUMMARIZE (
Data,
Data[Track],
Data[Date],
Data[Time],
"Total_km", DISTINCT ( Data[Kilometers Column] )
),
[Total_km]
)
Then, change [Revenue/Km] measure:
Revenue/Km = DIVIDE([Revenue], [Total Km])
Result:
The measure correctly calculates km on both subtotal and total levels.
The way it works:
First, we use SUMMARIZE to group records by trips (where trip is a unique combination of track, date and time). Then, we add a column to the summary that contains km for each trip. Finally, we use SUMX to iterate the summary record by record, and sum up trip distances.
The solution should work, although I would recommend to give more thoughts to the data model design. You need to build a better star schema, or DAX will continue to be challenging. For example, I'd consider adding something like "Trip Id" to each record - it will be much easier to iterate over such ids instead of grouping records all the time. Also, more descriptive names can help make DAX clean (names like km[km] look a bit strange :)

DAX/Power Pivot: Calculate 75% Expended Date from Cumulative Total

I have three tables that contain cost: Forecasted Cost, Actual Costs, Invoiced Costs. Each has an "EAC Filter" column for a Y/N of whether to include the cost in an Estimate at Completion, which automatically changes over time and/or as data is added. Here are examples:
EAC from the three tables can be calculated as follows:
Total Cost = Sum(Forecast[Cost])+Sum(Actual[Cost])+Sum(Invoice[Cost])
EAC = Calculate([Total Cost],EAC_Filter[EAC Filter]="Y")
I have a budget at the "Account" level, which could also be rolled up to a "Dept" level budget.
I need a measure for my Power Pivot table which will display the week at which costs have exceeded, or are forecasted to exceed 75% of the budget, using some sort of a cumulative cost, combined with the max week where cumulative cost >= .75 * Budget.
The weeks are numbered through the year as follows:
Thanks for your help!
Given an EAC measure which sums the cost per week,
EAC = CALCULATE(SUM(Forcast[Cost]) + SUM(Acutal[Cost]) + SUM(Invoice[Cost]),
EAC_Filter[EAC Filter] = "Y")
You can create a Cumulative Cost measure as follows:
Cumulative Cost = CALCULATE([EAC],
FILTER(ALL('Calendar'), 'Calendar'[Week] <= MAX('Calendar'[Week])))
Using this, we can create a measure that predicts the week the cost exceeds 75% of the budget:
75% Week = MINX(FILTER(ALL('Calendar'), [Cumulative Cost] > 0.75 * SUM(Budget[Budget])),
'Calendar'[Week])
Here's what the relationships structure looks like:

Count number of sporadic events matched to reoccuring events [excel]

I have two tables of different length:
a table of monthly data (e.g.: value of inventory at start of month)
a table of sporadic events which happened at random point throughout the year (e.g.: truck delivery to storage)
In table one, I would like to count in an extra column, the number of events from table 2 that occurred in that month. The table with the value of the inventory would show a count per row of how many trucks were unloaded.
I've been fighting with countifs but I just cannot get it to work due to different table lengths, the weird way to enter criteria etc.
I've tried to match the month and year of a truck delivery with the period in the inventory table.
=COUNTIFS(
<range: Dates of Truck deliveries from Table2>, "=MONTH(" &
<cellOfInventoryDate> & ")",
<range: Dates of Truck deliveries from Table2>, "=Year(" &
<cellOfInventoryDate> & ")")
I have a feeling there is a simple solution to this an dI just hit a wall.
Thanks
Table
1 - Inventory at start of month
01/01/2015 1000
01/02/2015 1200
01/03/2015 1100
01/04/2015 900
...
Table 2 - Date of Truck Delivery
01/01/2015
04/02/2015
07/02/2015
03/04/2015
11/07/2015
Ok, so here the answer.
I created a helper column in Table 2 which normalises the dates to the first of the month
=EOMONTH(cellwithdate,-1)+1
then I used countif (not countifs) to count when the date of the inventory matches the helper column.
=COUNTIF(helpercolumn, dateOfInventory)
this then counts how many deliveries were made in the month of that inventory's month.

how Calculate Margin in GP

I want to Calculate profit margin from Dynamics GP Database.
Which fields or table been used. and How Can I do that.
If any one have an idea please share with me.
In general, there are many different ways to calculate gross profit margin. Be sure you are using the method which is accepted by your companies accounting policies.
Here is an example which looks at all invoices which have been posted year to date and calculates the gross profit margin percentage.
Assuming gross profit margin = total profit / total revenue.
SELECT ( SUM(SUBTOTAL) - SUM(EXTDCOST) ) / SUM(SUBTOTAL)
FROM SOP30200 t1
WHERE t1.SOPTYPE = 3
AND t1.DOCDATE BETWEEN '1/1/2013' AND GETDATE()
This will return a decimal number like .44323. In that case you would be making an average gross profit margin of 44% for every invoice year to date.
SOP30200 = posted sales transaction documents

Resources