Price change Tracker - powerbi

I have problem where my power bi data is sourced from sql where we have product with price
but price will change of the product at future dates . now how would i create a DAX query which will help me in viualize the profit and track of price change
sample data:

Here's an option using DAX measures that will allow you to show the price change monthly, quarterly, or annually:
average price = AVERAGE(Table[Price])
% Price Change = IF(NOT(ISBLANK([average price])),
VAR CurrentValue = [average price]
VAR PreviousValue =
SWITCH(
TRUE(),
ISINSCOPE(Table[Date].[Month]), CALCULATE([average price], PARALLELPERIOD(Table[Date],-1,MONTH)),
ISINSCOPE(Table[Date].[Quarter]), CALCULATE([average price], PARALLELPERIOD(Table[Date],-1,QUARTER)),
ISINSCOPE(Table[Date].[Year]), CALCULATE([average price], PARALLELPERIOD(Table[Date],-1,YEAR))
)
RETURN
DIVIDE(
CurrentValue - PreviousValue,
PreviousValue
))

Related

Fetch specific record value from a table in PowerBI

I'm trying to dynamically calculate the % Change of the prices in a table, where each price will be compared with the first price (Price in the first record, ordered by date). There is a date filter, so the first record being displayed will change accordingly. Date column values are unique.
Assume the user applies a filter for date BETWEEN '15-APR-2021' AND '30-APR-2021'. Then the expected '% Change' column would look like:
For this example, I had to hardcode the starting price in the calculation:
% Change = (('table1'[price]/3218.95) -1)*100
I tried the below, but it doesn't return a static value of 3218.95. Instead, it re-calculates it at record level rather filtered table level as we see in the above screenshot :
first_date = (MIN(table1[Date]))
first_price = LOOKUPVALUE('table1'[price],table1[Date],'table1'[first_date])
% Change = (('table1'[price]/first_price) -1)*100
I'm new to PowerBI DAX. Logically the SQL would look like so:
SELECT
date,
price,
((
price /
( -- Gets the first price
SELECT price FROM table1
WHERE date IN (SELECT MIN(date) FROM table1 WHERE date BETWEEN '15-APR-2021' AND '30-APR-2021')
)
)-1) * 100 as '% change'
FROM table1
WHERE date BETWEEN '15-APR-2021' AND '30-APR-2021'
IF you want to get the first price you can use the following DAX:
first_price = CALCULATE(MIN('table1'[price]), FILTER( 'table1', MIN(table1[Date])))
As for the % Change:
% Change =
var curPrice = 'table1'[price]
var first_price = CALCULATE(MIN('table1'[price]), FILTER( 'table1', MIN(table1[Date])))
return ((curPrice/first_price) - 1) * 100

Calculating percentages including subtotal as a measure in Power BI

I have a dataset of population numbers by age group and gender (male & female). I want to make a matrix table which will show the numbers and percentages for the age groups and gender + column subtotals to show the total Persons (male + female).
I have created a measure to calculate percentage. However, when I apply this formula it is showing the correct percentages for male and female but for persons which is a subtotal of male & female it is adding up the percentages. Can someone please let me know how to get the correct percentages in the subtotal column. Thank you.
Measure =
DIVIDE(
sum('LGA ERP'[ERP]),
CALCULATE(
SUM('LGA ERP'[ERP]),
filter(
ALLSELECTED('LGA ERP'),
'LGA ERP'[Sex] = max('LGA ERP'[Sex])
)
)
)
Please see attached a copy of my Power BI file - https://1drv.ms/u/s!AubIV2PXG9p4gql5E4XrkIW4uDyY9A?e=HSTcyl
Try with this below measure. This will solve your issue.
Measure =
DIVIDE(
SUM('LGA ERP'[ERP]),
CALCULATE(
SUM('LGA ERP'[ERP]),
FILTER(
ALLSELECTED('LGA ERP'),
'LGA ERP'[Sex] = MAX('LGA ERP'[Sex])
|| 'LGA ERP'[Sex] = MIN('LGA ERP'[Sex])
)
)
)
Here is the final output-

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-

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:

How to calculate Cumulative Sum in Power BI

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: