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
Related
I have table Company. I need to display the TOP 6 recently created.
Exemple of data:
Expected results
What I tried :
Rank =
VAR d = Companies[CreatedDate].[Date]
RETURN
CALCULATE (
RANK.EQ ( d, Companies[CreatedDate], DESC)
)
the calculated column returned wrong values:
I need to order by creatd date, and when it's the same date, I need to order by Company Namr
How to correct it!,
For ranking based on created date and company name try the following steps, and if it helps accept it is as answer.
Create a calculated column for ranking the company name.
Company Sort = RANKX(ALL('Table'), 'Table'[Company Name], , ASC)
Create another calculated column for ranking using following DAX
Ranking on Date and Company Name =
VAR X = MAX('Table'[Company Sort])
var res =
RANKX(
ALL('Table),
'Table'[Created Date] * X + 'Table'[Company Sort]
)
RETURN res
Sort by using the column `Ranking on Date and Company Name'.
If you want to create a visual, then just add the column `Ranking on Date and Company Name' in the filter pane and select the Top N filter accordingly.
My challenge has been how to
01 - set Initial Inventory (considering the first day of the column);
02 - From the second day on, have Forecast (Previsão) - Sales (Vendas) = Balance. So 3 columns for each day
The way it is now, it repeats Initial Inventory throughout the days.
Here's the goal I'm trying to achieve:
Here's how the tables are related:
I would try to approach it with an unrelated helper table, that would contain 2 columns and 4 rows. Here's an example based on Contoso data.
Here's the helper table. Column Item ID is used for sorting result columns as well:
And now comes the magic:
Helper measure =
VAR minDate =
CALCULATE ( MIN ( DimDate[Datekey] ), ALLSELECTED ( DimDate ) )
VAR curDate =
SELECTEDVALUE ( DimDate[Datekey] )
RETURN
SWITCH (
SELECTEDVALUE ( Helper[Item ID] ),
1, IF ( curDate = minDate, [Opening Inventory Qty], BLANK () ),
2, [Production Qty],
3, - [Sales Qty],
4, [Closing Inventory Qty]
)
Here's an explanation:
minDate is used to get initial date on the visual
curDate is used to get current date
Next I check, which column from the helper table the measure is calculated for using SWITCH function
using SWITCH, I return different formula, based on helper column type
for Opening inventory, I only return a value when current date is equal to initial date. Otherwise, I return BLANK() - that's why Opening inventory only appears on the first date
The matrix visual is built using Product name in rows; Year, month, day and Item (from the helper table) in columns, and the Helper measure from above.
Here's a the result (don't mind quantities, it's a sample data):
EDIT:
For completeness' sake, here's how the model looks. Notice that the Helper table is not related to any other table:
I am trying to calculate 'TimeSheet Team PV'[Time (h)] * 'Position History'[Salary]
Data model:
TimeSheet Team PV" table:
"Position History" table:
In the Position History table, I have some employees which were promoted and appear as having different periods during different periods in the company, with different salary. Can I join somehow these values based on Start Date, End Date (Position History table) and Work Date (TimeSheet Team PV tables) + Name?
I would like to generate new column in the TimeSheet Team PV table, with the calculation above.
You'll need to write some logic to make sure it uses the Work Date column. Something like this:
Cost =
VAR WorkDate = 'TimeSheet Team PV'[Work Date]
RETURN
'TimeSheet Team PV'[Time (h)]
* CALCULATE (
SELECTEDVALUE ( 'Position History'[Salary] ),
'Position History'[Start Date] <= WorkDate,
'Position History'[End Date] >= WorkDate
)
I have 4 tables Calendar, Products, Region, and Sales. Then I create the relationship between 4 tables through
Region.Region_code -> Sales.Region_Code
Product.Product_wid -> Sales.Product_wid
Calendar.Cal_Row_wid -> Sales.Date_wid
Then I create 2 slicers Date and Month:
I would like to write a measure to calculate the Total quantity_rcs (which belongs to table Sales) for all order from the beginning up to current date (which characterized by Date and Month; both of them belong to Calendars).
How this should work if you have multivalue slicer?
belove example for onevalue slicer where we are pointing to specific date (consider that you have a unique date column In calendar maybe "issue_date"):
Measure =
var __date = calculate(max(Calendar[issue_date]), FILTER(ALL(Calendar[Date]
,Calendar[Month],Calendar[Year]),
Calendar[Date] = SELECTEDVALUE(Calendar[Date]) &&
Calendar[Month] = SELECTEDVALUE(Calendar[Month]) &&
Calendar[Year] = YEAR(TODAY())
)
)
return
calculate( sum(Sales[quantity_rcs]),
FILTER(ALL(Calendar[issue_date]),
Calendar[issue_date] <= __date )
)
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-