How to remove blank dates from related table with DAX running total? - powerbi

I have the following table.
I want to make a running total of Qty sold, based on Date Sold. Products weren't sold until 2019.
It is linked to my date table here, notice the inactive relationship, as the main relationship is Date Added to Stock. So I will use USERELATIONSHIP for the running total:
I created a DAX running total like so:
Cumulative Sold =
CALCULATE(
SUM(Data[Qty Sold]), USERELATIONSHIP(CalendarTable[Date],Data[Date Sold]),
FILTER(
ALLSELECTED(CalendarTable[Date]),
ISONORAFTER(CalendarTable[Date], MAX(CalendarTable[Date]), DESC)
)
)
However when I add this data to a table, I can see that there are figures all the way back to 2018. In the first row the date is null, and has a sum of 441. I guess these are blank dates from 2018, and some from late 2022 perhaps. There were no products sold in 2018, it should be 0.
My calendar table goes back to 2018, is that the issue? How can I avoid this?
I tried to add a filter, however the resulting figures are completely wrong and is not cumulative.
Cumulative Sold =
CALCULATE(
SUM(Data[Qty Sold]),
FILTER(CalendarTable, CalendarTable[Date] >= MIN(Data[Date Sold])
USERELATIONSHIP(CalendarTable[Date],Data[Date Sold]),
FILTER(
ALLSELECTED(CalendarTable[Date]),
ISONORAFTER(CalendarTable[Date], MAX(CalendarTable[Date]), DESC)
)
)
Any advice? Perhaps my DAX measure is incorrect?

Related

Average with Multiple Criteria in PowerBI

I have 4 columns in table called year, product, status, value. I want the MEASURE average of value only for specific status and for specific year so how i can write the average DAX with multiple criteria.
In excel i can easily write as averageifs(value, year=2021, status="Sold").
I WANT THE MEASURE ONLY.
Please suggest. i tried every and did not get success to add multiple criteria's.
Regards,
SK
You can try this below measure-
average_ =
CALCULATE(
AVERAGE(your_table_name[value]),
FILTER(
ALL(your_table_name),
your_table_name[year] = 2021
&& your_table_name[status] = "sold"
)
)

DAX Grouping Evaluation

In this measure - I'm creating a temp table to group by customer ID and return the year of the min order_date for each customer. I then want to count the # of customers that show up for a given year (basically just a row count).
What I'm struggling to understand is - this formula doesn't seem to look at the SUMMARIZE table within it. If I put year and this measure in a matrix, it counts based on the raw table, not the grouped version built by SUMMARIZE in the formula. Any ideas why it's evaluating this way?
COUNTROWS(
SUMMARIZE(
Orders,
Orders[Customer ID],
"min_order_date_year",
MIN(Orders[Order Date].[Year])))
The formula is OK as per logic you provided to it. You have to clear about both your requirement and what your are writing in your code. Your formula is returning exactly what you are instructing it.
If I understand correct, you simply need the Customer count for the Minimum Year. For say if you have 6 unique customer for Year 2019 and 11 unique customer for Year 2020, you are looking here for value 6 to return by your measure.
Now, what your summarize table actually returning? if you create a separate custom table for the Summarize code only as below, you can see the table will actually hold all your customer name/id in first column and the second column will hole the MIN year available for that customer.
orders_summarize =
SUMMARIZE(
Orders,
Orders[customer id],
"min_order_date_year",MIN(Orders[Order Date].[Year])
)
So basically you have list of all customer in your summarize table. And now your are counting rows of your summarize table which is actually returning the total number of unique customers.
Finally, if you wants customer count for a specific Year (like MIN year), follow these below steps-
Step-1: Create a custom summarized table as below-
store_summarized_table =
SUMMARIZE(
store,
store[Customer ID],
"mindate",MIN(store[Order Date])
)
Step-2: create a measure as-
count_cust_id = COUNT('store_summarized_table'[Customer ID])
Step-3: Now configure your Matrix visuals as shown in the below image. You can also get the output in the image-
To avoid the Physical Table, you can do this below-
Step-1: Create a Custom Column as below-
is_min_year =
// -- keep current row's customer id to a variable
VAR current_cust_id = store[Customer ID]
// -- keep current row's YEAR value to a variable
VAR current_year = store[Order Date].[Year]
// -- find the MIN YEAR from order date for the current row customer id
VAR min_year_current_custommer_id =
CALCULATE(
MIN(store[Order Date].[Year]),
FILTER(
store,
store[Customer ID] = current_cust_id
)
)
// -- check the current row's year is the MIN year of order date for the customer as well or not.
RETURN IF(current_year = min_year_current_custommer_id, 1,0)
OR create a measure as-
is_min_year_measure =
VAR min_order_year_for_current_customer =
CALCULATE(
MIN(store[Order Date].[Year]),
FILTER(
ALL(store),
store[Customer ID] = MIN(store[Customer ID])
)
)
RETURN
IF ( MIN(store[Order Date].[Year]) = min_order_year_for_current_customer, 1,0)
Step-2: Create a Measure as below-
For created Custom Column
count_cust_for_min_year =
CALCULATE(
DISTINCTCOUNT(store[Customer ID]),
FILTER(
store,
store[is_min_year] = 1
)
)
For Created Measure
count_cust_for_min_year =
CALCULATE(
DISTINCTCOUNT(store[Customer ID]),
FILTER(
store,
[is_min_year_measure] = 1
)
)
Now add "Order Date" and measure "count_cust_for_min_year" to your Matrix. The out put will same as below-

DAX calculation - SUM sales the latest 2 days

I need your help.
I have a table (“Table”) like this.
In the table below I have SUM “sales” by the LATEST 2 days with sale (not the latest 2 DATES! i.e. example: if the latest sales update is on a Tuesday, it sums the sale for Monday and Friday (no sale in weekend)) for each products.
in other words:
The calculation is made with the following DAX calculated column:
Sale last 2 days=
VAR ProductDates =
CALCULATETABLE (
VALUES ( Table[Date]),
ALLEXCEPT ( Table, Table[Product_ID])
)
VAR LastTwoDates = TOPN ( 2; ProductDates;[Date] )
RETURN
CALCULATE (
SUM ([Sale]);
ALLEXCEPT ( Table, Table[Product_ID] );
Table[Date] IN LastTwoDates)
Now, I need to take it a step further:
What I want to do is to make a new calculations which SUM the sale for each product for the latest 2 days, but ONLY for the Distributors, where the "Distributor indicator"=1. And the latest 2 sales days in question, are the sales days where there has been sale to these distributors only.
(example: if the latest sales day is a tuesday and there were no sale from these distributors yesterday, the the latest two days will be previous friday and thursday (i.e. the latest 2 days where sales is not null).
I know I can use the calculation, I have already made, but I can’t figure out where to put the logic in, in order to get the right result:
Example:
I know I can use the calculation, I have already made, but I can’t figure out where to put the logic in, in order to get the right result:
Can some of you please help!
Thanks. It is greatly appreciated.
Br,
Jakob
Assuming there is a date dimension table named "Calendar", this worked for me.
=IF(
COUNTROWS(
INTERSECT(
VALUES('Calendar'[Date]),
CALCULATETABLE(VALUES(Table[Date]), ALL(Table))
)
) > 0,
CALCULATE(
SUM(Table[Sale]),
CALCULATETABLE(
TOPN(2, SUMMARIZE(Table, Table[Date]), Table[Date], DESC),
FILTER(ALL(Table), Table[Date] <= MAX('Calendar'[Date]))
),
ALL('Calendar')
)
)

POWER BI - Create a measure to compare Forecast vs Sales

Create a measure to compare forecast vs sales in power BI.
The logic is that at the beginning of Jan. 2018 it is forecasted the sales for the whole year and the amount of pcs sold in Jan. 2018 is only known in Feb. 2018 (next month's file). As an example, we are in Nov. 2018 and it is known how many pcs were sold in the previous months. I want to create a table to compare forecast vs sales.
My dataset looks like the figure below. It is marked in red the historical data.
An example of a power BI matrix that I am trying to create by dividing forecast by sales
It looks like you need to normalize your data source. This may look something like:
Now you can create some measures. Create a Forecast Qty measure:
Forecast Qty:=CALCULATE (
SUM ( SourceTable[Qty] ),
SourceTable[Type] = "Forecast"
)
A Sold Qty measure:
Sold Qty:=CALCULATE (
SUM ( SourceTable[Qty] ),
SourceTable[Type] = "Sold"
)
And a Sales % Forecast Measure:
Sales % Forecast:=DIVIDE (
[Sold Qty],
[Forecast Qty],
BLANK()
)
Now you can lay out your visualisation as you choose. For example:

Months To date formula not working properly on Power Bi

I am newbie in Power Bi. I am calculating months to date of a measure.
I have written following DAX formula for that,
MTD in Sales = CALCULATE([Total Sales], DATESMTD(Dates[Date]) )
it shows me correct total sales value of this month.But when i make day-wise, it shows me some unrealistic value.
I have attached a screenshots of my result..Please have a look.
I don't understand what wrong are going on? Can you please find out the problem plz?
DATESMTD(Dates[Date]) is equivalent to:
CALCULATETABLE(
FILTER(
ALL(Dates[Date]),
AND(
Dates[Date] <= MAX(Dates[Date]),
AND (
YEAR(Dates[Date]) = YEAR(MAX(Dates[Date])),
MONTH(Dates[Date]) = MONTH(MAX(Dates[Date]))
)
)
)
)
This only considers the maximum value of the dates in the external filter context, so for Tuesday (today), it will contain every day of the month up to today, for Monday (yesterday) it will contain every day of the month up to yesterday and so on. (Assuming that no Sales are linked to future dates).
If you want to further filter this to only include sales that occurred on the given day of the week, I'd suggest altering MTD in Sales to:
[MTD in Sales] := CALCULATE([Total Sales], DATESMTD(Dates[Date]), Dates[DayOfWeekName])
This will additionally filter the included dates to only those with DayOfWeekName values present in the external filter context.