How to rank based on a field where the value is the same. There is some more ranking applied here and this scenario need to be addressed as well as I can not rank again by Sales field. Instead, I need to say:
If Unit is the same on the list of territories, rank based on Sales.
Example:
Terr Sales Unit Should look like : Terr Sales Unit
---- ------ ----- -----------------------
Boston 1 5 Maine 10 5
Maine 10 5 Boston 1 5
Often a mathematical approach works well for this. First, without wanting to patronise, it's possible to use a discrete (blue) measure to sort data. Place the sorting pill to the far left on the Rows and the table will sort according to this pill.
Ok, so the formula. Without knowing how large the Sales figure can go, you want to create a calculation that would give the highest value to that you want to appear top.
For example perhaps multiple Unit by 1,000,000 and add Sales. Just make sure the Units are multiplied by a number large enough to make Sales inconsequential.
This field may work, depending how large the Sales figure can go:
[SortField] = (SUM([Unit])*1000000 + SUM([Sales])) * -1
Put the field to Rows, convert to Discrete, then place to the far left. If the sorting is correct hide the field header.
It multiplies by -1 to sort descending.
Related
I have the following measure:
test = SWITCH(TRUE(),
MAX(test[month])>=9&&MAX(test[month])<=12,"fall",
MAX(test[month])>=1&&MAX(test[month])<=3,"winter",
MAX(test[month])>=4&&MAX(test[month])<=6,"spring",
MAX(test[month])>=7&&MAX(test[month])<=8,"summer")
Currently it looks at the month number (i.e. "3" for March and outputs "winter", what I'd like however is it to output is a count per season to show the distribution of the seasons in the dataset.
For example my desired output would be
Month Number
Count of occurrences of each season
fall
5
winter
7
spring
11
summer
2
I can't have a calculated column here either as I will want to make this measure dynamic later on with the use of a slicer, can someone tell me if this is possible?
The issue here is that you want to define your categories within the measure. Measures are not dynamic without some filter-context.
Take this for example:
Notice that the output of the calculation is identical between seasons.
There is no filter context to help the measure discern between the different seasons because these seasons are not defined in the model. (At least, I don't know how to make this work)
Switch returns the first true result. So, if you have values like in your sample, then start with the smallest, then bigger, and the largest at the end.
test =
SWITCH(
TRUE()
,MAX(test[month])<4,"winter" -- test <4
,MAX(test[month])<7,"spring" -- 3< test < 7
,MAX(test[month])<9,"summer" -- 6< test < 9 -- Is it ok that you have 2 months in
,"fall" -- 8< test -- summer and 4 in fall?
)
If you use MAX(test[month])<4,"winter" instead of MAX(test[month])<=3,"winter" then you avoid one calculation step and the code will be faster.
Then you need to use the result to find months numbers and get dates from the selected months. Then calculate your table filtered by months dates. If this answer is not enough to solve the case, then give more information about you table, it's columns, and what do you mean by 'Count of occurrences of each season', exactly what does 'occurrences' mean, is it a number of certain rows or some unique values.
Imagine a tiered revenue sharing scheme like this:
Revenue up to 10000 get 100% of it.
Revenue up to 12000 get the above plus 80% of the amount above 10000.
Revenue up to 14000 get the above plus 60% of the amount above 12000.
Revenue up to 16000 get the above plus 40% of the amount above 14000.
Revenue over 16000 get the above plus 20% of the amount above 16000.
E.g. A revenue of 13000 will get you a share of 10000+0.82000+0.61000 = 12200.
I tried making a table (each threshold a column) and calculate the individual fractions using IF clauses and then add them all up. It is very cumbersome. I would like to use only two cells with the entire calculation done in one cell, hard-coded.
If at all possible, extra bonus points if I can have the threshold values (10000, 12000, etc) and fractions (100%, 80%, etc.) in separate cells as parameters for the calculation, maybe something like an array-function?
Thank you very much in advance!
Start your lookup table with a value of 0 and a rate of 100%. In this case, VLOOKUP() with the last parameter equal to 1 will correctly find the required row.
In order not to recalculate all the above rows for each of the values, calculate them in advance and place them in the table as an additional column.
For the first line it will be 0, and calculate all subsequent values using a formula like =C2+(A3-A2)*B2
For such a table, a not very complicated formula will return the correct result:
=(<revenue>-VLOOKUP(<revenue>;<lookup_table>;1;1))*VLOOKUP(<revenue>;<lookup_table>;2;1)+VLOOKUP(<revenue>;<lookup_table>;3;1)
The third parameter in the VLOOKUP() functions increases from left to right: 1 - the base amount, 2 - the interest rate, 3 - the calculated markup for reaching the previous levels.
For the data shown in the figure, the formula is used
=(E2-VLOOKUP(E2;$A$2:$C$7;1;1))*VLOOKUP(E2;$A$2:$C$7;2;1)+VLOOKUP(E2;$A$2:$C7;3;1)
I have an explore like the following -
Timestamp Rate Count
July 1 $2.00 15
July 2 $2.00 12
July 3 $3.00 20
July 4 $3.00 25
July 5 $2.00 10
I want to get the below results -
Rate Number of days Count
$2.00 3 37
$3.00 2 45
How can I calculate the Number of days column in the the table calculation? I don't want the timestamp to be included in the final table.
First of all— is rate a dimension? If so, and you have LookML access, you could create a "Count Days" measure that's just a simple count, and then return Rate, Count Days, and Count. That would be really simple.
If you can't do that, this hard to do with just a table calculation, since what you're asking for is to change the grouping of the data. Generally, that's something that's only possible in SQL or LookML, where you can actually alter the grouping and aggregation of the data.
With Table Calculations, you can make operations on the data that's been returned by the query, but you can't change the grouping or aggregation of it— So the issue becomes that it's quite difficult to take 3 rows and then use a table calculation to represent those as 1 row.
I'd recommend taking this to the LookML or SQL if you have developer access or can ask someone who does. If you can't do that, then I'd suggest you look at this thread: https://discourse.looker.com/t/creating-a-window-function-inside-a-table-calculation-custom-measure/16973 which explains how to do these kinds of functions in table calculations. It's a bit complex, though.
Once you've done the calculation, you'd want to use the Hide No's from Visualization feature to remove the rows you aren't interested in.
I'm sure the nature of my question will reveal I am a rookie at PowerBI. -- so, please, don't hesitate to write like you're writing to a 10 year old. I have a table with several records per person (sorted by person). Each record has a Weight. I want my PowerBi report to display only one record per person with the sum of all that person's weights. There might be a wrinkle in that the weight is in a different table (two tables away via two relationships). Since I am in learn mode, don't mind if solution comes in progressively more difficult steps (as the weight table gets further away from the people table).
Data model:
- 'PersonTable' related to 'TestTable' by 'Name'
- 'TestTable' related to 'WeightTable' by 'Test'
I have tried creating a new column (not measure) in the PersonTable (and I have tried putting a new column in the TestTable) and then using various formulas to try to sum the weights in the WeightsTable - to no avail. The summed weights always come back including the sum of all possible weights in the WeightsTable.
Here is some contrived test data and expected results....
* Name Test Weight
* ------ ----- ------
* Dave TestA 3
* Dave TestC 5
* Dave TestE 7
* Harold TestA 3
* Harold TestB 4
* Jack TestD 6
* Jack TestE 7
Desired Results:
* Dave 15
* Harold 7
* Jack 13
I have concocted a "codeless solution" using the groupby feature. I am dissatisfied with it though because I believe this approach will not allow me to "drilldown" through one of the aggregated records produced to see the detailed records that were grouped. That will be the subject of my next question.
Here is the "solution"...
Use the "Query Editor" to modify the 'TestTable'.
Merge the query for the 'WeightTable' into the query for the 'TestTable' keeping only the weight column from the merged in table.
Duplicate the new column. (I thought I would want two columns so that I could keep the un-grouped weights for detail reporting, but this has not been the case yet.
Push the "GroupBy" button on the ribbon for the "Home" tab and fill out form as follows...
Radio Button - Basic
Group by: "Name"
New Column Name: "Sum of Weights"
Operation: "Sum"
Column: "Weight-Copy"
This generated the values I am looking for but removed the columns holding the detail data in the 'TestTable' which I will need for later (possibly drillthrough) reporting. So I continue to seek a better solution...
I believe you've chosen a proper solution, up until step 4. Remove that step and load the joined dataset into the data model.
Then, just drag and drop the desired columns onto the canvas (weight and name). PowerBI will automatically sum the weights and then group that by name, producing your desired output.
I am pretty new to MDX but I know what I want accomplish but its proving very hard. Basically, I have a dataset where each row is a sale for a customer. I also have postcode data and the UK population at each ward.
The total population in each ward is then divided by the count of the wardcode within the data set - e.g. ward A had a population of 1,000. I have ten customers who live in ward A and so the population value is therefore 1,000/10.
So as long as there are no other dimensions selected, only the region hierarchy, I can then drill up and down and the population penetration as count of customers / calculated population value is correct. However, as soon as I introduce more dimension the total population will not sum to its true value.
So I therefore need to do the calculation above within the cube and I am trying to find the MDX function(s) to do this.
Esentially something like -
step 1) sum the number of ward codes (the lowest level of the Geographic hierarchy) and group this by the distinct ward code, eg wardcodeA = 5, wardcodeB=10 etc.
Step 2) Then take the population in each ward (which could be stored as the total at ward level and taking the average) and then divide this by the result of the previous step
step 3) sum the results from each ward at the currently select Geographical level
The fact other dimensions are changing the value of customers / population means that something in your modeling is wrong.
You should have a fact table (can be a view/concept) like this :
REGION_ID, CUSTOMER_COUNT, POPULATION_COUNT
Once you got this create a fact table and a specific measure for counting customers and population with a single dimension linked. This is the main point, do not link your measures with dimension that are not needed.