django DenseRank annotation based on another annotate - django

I want to annotate rank queryset based on avg of points.
annotate(avg=Avg('point'), rank=Window(expression=DenseRank(), order_by=Value("avg").desc())
but rank is 1 for all data.
When I check query straight in database result is true! but in django all rank is 1.
When I use F("avg") instead of Value("avg"), result is true but query is not optimal . With F() in query once calculate average of points for avg and calculate avg again for rank avg:
query 1:
SELECT AVG(point) AS avg,
DENSE_RANK() OVER (
ORDER BY AVG(point) DESC
) rank,
but I want below query:
query 2:
SELECT AVG(point) AS avg,
DENSE_RANK() OVER (
ORDER BY avg DESC
) rank,
when I user Value('avg') query 2 generate but all rank is 1 and when I use F('avg'), result is true but query 1 generate which is not optimal and calculate 'avg' twice(twice AVG(point) in query).

Related

DAX - DISTINCTCOUNT based on measure

I have the following dataset:
and the following measures:
Count of Filters = if(ISFILTERED(Table1[Product]), COUNTROWS(FILTERS (Table1[Product])), 0)
Count = if(ISFILTERED(Table1[Product]), COUNT(Table1[Unique_ID]),0)
What I would like to to is, when I filter the table at Product, that a measure disctinct counts the unique_ID that have a count equivalent to the count of filters.
The following should be able to explain better:
When filtering to product 1, A list of all appear, because they all have product 1:
But when also selecting Product 2, A and B have a total count of 2 because they have entries for both product 1 and 2.
Now, what I ultimately want, is a measure that can do a distinctcount on only the Unique_ID that have the Count equal the count of filters. In the above scenario, I would want the total to give me 2 (as only UNIQUE_ID A and B have both products 1 and 2).
I have put together the following:
Total = CALCULATE(DISTINCTCOUNT(Table1[Unique_ID]),FILTER(Table1, Table1[Count]=Table1[Count of Filters]))
but this doesn't seem to work. I understand why it's not working, but I can't seem to figure out how to put together a measure to calculate this for me.
Many thanks in advance!

Getting equivalent min percentage year

I have the data bellow that shows year, value and cumulative percentage
create table #data (cost money, Current_Year int, Cumulative money, [Percentage] decimal(9,4))
insert into #data values
(43000000,2014,43000000,0.000647922726779209),
(54000000,2015,97000000,0.00146159312785077),
(60777777.75,2016,157777777.75,0.00237739088337079),
(123777777.8,2017,281555555.55,0.0042424707742277),
(126555555.5,2018,408111111.05,0.00614940613721881),
(60761257937,2019,61169369048.05,0.921698241604101),
(492914890.8,2020,61662283938.85,0.929125468583571),
(456692522.2,2021,62118976461.05,0.936006897985512),
(416673457.6,2022,62535649918.65,0.942285321307017),
(422831144,2023,62958481062.65,0.94865652846487),
(371598857.2,2024,63330079919.85,0.954255769042197),
(346364399,2025,63676444318.85,0.959474777550553),
(336826238.7,2026,64013270557.55,0.964550065341932),
(350483170.1,2027,64363753727.65,0.969831135371267),
(334551349.4,2028,64698305077.05,0.974872145198032),
(310619368.1,2029,65008924445.15,0.97955254863919),
(344523398.6,2030,65353447843.75,0.984743817008031),
(345189362.5,2031,65698637206.25,0.989945120101198),
(332918789.2,2032,66031555995.45,0.994961530559209),
(334382754.3,2033,66365938749.75,1)
select * from #data
drop table #data
I want to be able to select a percentage in my case between 94% and 95% in my filter to return the equivalent min year for that that percentage and the percentage
I tried
95%Range = var result =IF([Cost constant %]>=0.94 && [Cost constant %]<=0.95,[Cost constant %],BLANK())
return
IF(result <>BLANK(),MAX(Calendar_Date[Current_Year])
it is not working
expected output
)
I was able to do these using two measures:
Min Year = min(Query1[Current_Year])
Percentage Lookup = LOOKUPVALUE(Query1[Percentage],Query1[Current_Year],[Min Year])
You can then add percentage to a filter or slicer and Power BI will return the earliest year that matches the filter criteria. Here are the results in a simple table visual:

How do I manipulate measure values based on 2 other dimension tables

Power BI newbie here and I'm trying to figure how to craft my DAX to manipulate my measure values based on certain criteria in the other two tables.
Currently I have 2 separate tables which are joined by a One to Many relationship and a separate Measures table. (Total Sales Price is computed as sum of Sales Price)
My aim is to create a new measure where Total Sales Price is multiplied by 1.5x when DIM_Product_Type[Product Category] = "High".
New Measure =
CALCULATE (
SUM ( FACT_PriceDetails[Sales Price] ),
FILTER ( DIM_Product_Type, DIM_Product_Type[Product Category] = "High" )
) * 1.5
However this returns no values in my visual and I'm trying to discern if its a matter of the table joins or the DAX expressions.
Thank you for your time!
Your measure seems good.
It will select only those products with a Product Category of "High" and multiply them by 1.5 to give you result. i.e. Give me the sum of all "High" Product category Price details multiplied by 1.5.
What you need to check is:
Product Serial Numbers match across the two tables
Your Product Category does indeed contain the category "High"
You have entries in FACT_PriceDetails that link to a DIM_Product_Type that has a category of "High"
Check you have not set any filters that could be hijacking your results (e.g. excluding the "High" product category product type or the realated fact/s)
Option-1
You can do some Transformation in Power Query Editor to create a new column new sales price with applying conditions as stated below-
First, Merge you Dim and Fact table and bring the Product Category value to your Fact table as below-
You have Product Category value in each row after expanding the Table after merge. Now create a custom column as shown below-
Finally, you can go to your report and create your Total Sales measure using the new column new sales price
Option-2
You can also archive the same using DAX as stated below-
First, create a Custom Column as below-
sales amount new =
if(
RELATED(dim_product_type[product category]) = "High",
fact_pricedetails[sales price] * 1.5,
fact_pricedetails[sales price]
)
Now create your Total Sales Amount measure as below-
total_sales_amount = SUM(fact_pricedetails[sales amount new])
For both above case, you will get the same output.

How to get the first value from a sorted table into a measure?

I have a DAX table with 2 columns RANK and VALUE. I need a measure that returns the VALUE for the row where RANK is the lowest.
Solution needs to be efficient as table has tens of millions of rows with distinct values in column VALUE.
I have tried using TOPN, but this returns a table, not a single value.
I have tried using MIN, but this would return the lowest VALUE, not the VALUE with the lowest RANK
I cannot use LOOKUPVALUE because in my real world example, the table has many more columns than those 2, and some of those columns would have filters applied to them by the client application, and obviously I don't know those filters at design time.
VALUE RANK MANY_MORE_COLUMNS ...
100 1 ...
200 2 ...
50 3 ...
In this case, I would need the value 100 to be returned, because lowest RANK is 1.
Try this:
Measure =
CALCULATE (
MAX ( Table[VALUE] ),
FILTER ( Table, Table[RANK] = MIN ( Table[RANK] ) )
)
The FILTER function filters 'Table' to the row with the lowest RANK. MAX(Table[VALUE]) then returns the VALUE in that row. Allthough there is only one row, you need to use a function here that returns a scalar value (MAX in this case). The CALCULATE function expects a scalar value as an argument here.

DAX RANKX: Why does it return 1?

I am using AdventureWorks DB and I want to rank the resellers by their total sale amount. So basically the table looks like this:
I used the following measure to rank it:
Rank Reseller = RANKX(ALL(ResellerSales), MAX(ResellerSales[SalesAmount]),,1)
But it returns only 1 as the rank.
I followed these two videos:
https://www.youtube.com/watch?v=z2qzJVeYhTY
and
https://www.youtube.com/watch?v=SsZseKOgrWQ&t=603s
but I can't understand what is wrong!
This has to do with the current scope that Rankx evaluates de Aggregation.
Try wrapping your aggregation with CALCULATE, and you probably want the SUM not the MAX:
Rank Reseller = RANKX(ALL(ResellerSales), CALCULATE(SUM(ResellerSales[SalesAmount])))
You can create a Measure like so, and use it on RANKX, since it is a measure it will work without explicitly adding the CALCULATE:
Sales Amount = SUM(ResellerSales[SalesAmount])
Rank Reseller = RANKX(ALL(ResellerSales), [Sales Amount])
EDIT:
Rank Reseller = RANKX(ALL('ResellerSales'[Resellerkey]), [Sales Amount])
Try it like this.
To rank the [ReSellerkey] by [SalesAmount] you'd want to do something like this:
Rank Sales Amount :=
RANKX(
'Table',
'Table'[SalesAmount],
,
ASC,
Dense
)