Rank by created date and company name - powerbi

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.

Related

Get min date from another table with filter

I got two tables. There is a relationship between id and user id column. I want to add a calculated column in the user table with the first transaction of the user, for invoice type 'A'.
When I use:
Calculate(min(Transaction[transaction date])) it works fine, but I need to filter on invoice type. When I use Calculate(min(Transaction[transaction date]),Filter(Transaction,Invoice type="A")) I only get 2021-02-01 and the realtionship does not work.
What is the most efficent way to achieve this?
User table:
ID
Name
1
John Smith
2
Alan Walker
Transaction table:
user id
transaction date
Invoice type
1
2021-02-01
A
1
2021-02-25
A
1
2021-02-25
B
2
2021-03-05
A
2
2021-01-23
B
Here is the correct DAX code for your calculated column, just drop the FILTER statement, since that changes your filter context within the CALCULATE to look at all rows where Invoice type = "A", regardless of User ID.
Minimum Date =
CALCULATE (
MIN ( 'Transaction'[transaction date] ),
'Transaction'[Invoice type] = "A"
)
Since you need context transition to provide you the row context from the Users table to the Transactions table, you can alternatively use this sort of filtering statement, where the table you are filtering on is also provided within the current filter context of your row in Users:
Min Date =
CALCULATE (
MIN ( 'Transaction'[transaction date] ) ,
FILTER (
CALCULATETABLE ( 'Transaction' ) ,
'Transaction'[Invoice type] = "A"
)
)

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

PowerBI new column calculation based on columns from different tables

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
)

DAX PowerBI SUM from the beginning up to current date (input by user)

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 )
)

Power BI slicer OR condition

The interaction between two slicers in Power BI gives me output with AND condition.
Example: If I selected the year 2020 and company ABC, the output would be all the data from company ABC in the year 2020.
But I want the two slicers to work with OR condition.
I have used this Dax
Include = (MAX(Table1[Column1]) = SELECTEDVALUE(Col1[Column1])) +
(MAX(Table1[Column2]) = SELECTEDVALUE(Col2[Column2]))
But the problem with above Dax I have not selected anything in slicer ( ALL by default) it is showing me a blank visual. What am I doing wrong?
let me guess you have a table "or_slicer_main_table" with Year, Company and some other columns. Now create 2 new table where the first one will contain the distinct list of Year from table "or_slicer_main_table" and the second one will contain distinct Company list from that same table.
New custom Table 1:
or_slicer_year_list =
SELECTCOLUMNS(
'or_slicer_main_table',
"YEAR", 'or_slicer_main_table'[year]
)
New custom Table 2:
or_slicer_company_list =
SELECTCOLUMNS(
'or_slicer_main_table',
"company", 'or_slicer_main_table'[company]
)
Do not establish any relation between those 3 tables.
Step-1: Create Year slicer using the newly created "or_slicer_year_list" table.
Step-2: Create Company slicer using the newly created "or_slicer_company_list" table.
Step-3: Create these following 5 measures in your table "or_slicer_main_table"
1.
year_current_row = max('or_slicer_main_table'[year])
2.
year_selected_in_slicer = SELECTEDVALUE(or_slicer_year_list[YEAR])
3.
company_current_row = max('or_slicer_main_table'[company])
4.
company_selected_in_slicer = SELECTEDVALUE(or_slicer_company_list[company])
5.
show_hide =
if(
[year_selected_in_slicer] = [year_current_row]
|| [company_selected_in_slicer] = [company_current_row],
1,
0
)
Now you have all instruments ready for play. Create your visual using columns from the table "or_slicer_main_table"
Final Step: Now just add a visual level filter for the measure "show_hide" and set value will show only when "show_hide = 1".
The final output will be something like below image-
Can you try using "IN VALUES" instead of "SELECTEDVALUE"
So your DAX should be
Include = (MAX(Table1[Column1]) IN VALUES (Col1[Column1])) +
(MAX(Table1[Column2]) IN VALUES (Col2[Column2]))
SELECTEDVALUE function returns the result only if single value is selected in slicer in case of multiple selection it will return Blank(). Thats in the case when nothing is selected (which is similar to all selected) has multiple values in set and so SELECTEDVALUE fucntion will return Blank(). This can be handled by using "IN VALUES" function which can return a set of all selected values.