I'm new to Power BI and have an assignment to hand in for school. Awaiting for 365 admin to grant access to power BI community I'm hoping someone can help me explain the following:
In this basic Table called Population, there is data over 10 years for each country and their population.
I would like to add a column and with DAX formula calculate the increase relative to last year.
So I would think the basic idea is something like
Increase = CALCULATE(SUMX(population, Filter(Population, Population[Year] = Population[year] + 1) - Population[Population))
But I'm not finding the right formula.
Any help would be much appreciated.
calculated_column:
NewColumn_calculate = population[Population]-CALCULATE(SUM(population[Population]), FILTER(population, EARLIER(population[Country]) = population[Country] && EARLIER(population[Year]) = population[Year] + 1))
NewColumn_sumx = population[Population]-SUMX(FILTER(population, EARLIER(population[Country]) = population[Country] && EARLIER(population[Year]) = population[Year] + 1), population[Population])
measure:
Measure = SUM(population[Population]) - SUMX(FILTER(all(population), population[Country] = MAX(population[Country]) && population[Year] = MAX(population[Year]) - 1), population[Population])
Related
Hi I have desperately been trying to work this out and have referred to several posts but am still not getting the correct answer!
I have a bunch of providers of different provider type. I calculate an average cost change for each provider (from more granular payment data). I then want to find the standard deviation of these provider level changes for the difference provider type.
This is where I've got up to with the dax - this gives the same standard deviation across all provider types rather than the required output.
group_test =
var tab1 = SUMMARIZECOLUMNS(ProvData[Provider Type],ProvData[Provider Code], "prov_avg",AVERAGEX(core_data, sum(PayData[Payment1])-sum(PayData[Payment2]))/SUM(PayData[Payment1]))
var sd_type = SELECTCOLUMNS(SUMMARIZE(tab1,[Provider Type],[Provider Code], "test", STDEVX.S(tab1,[prov_avg])), "sd_type", [test])
var tab2 = ADDCOLUMNS(tab1, "sd_type", sd_type)
return tab2
I want my final table to look like this
Provider Code
Provider type
Prov_avg
sd_type
1
a
x
sd for a
2
a
y
sd for a
3
b
z
sd for b
Thanks in advance for any help
Add a column to your table:
stdColumn =
var prov_Code = ProvData[Provider Code]
var prov_type = ProvData[Provider Type]
var stdValue = CALCULATE (STDEV.S([prov_avg]), FILTER(prov_Code = ProvData[Provider Code] && prov_type = ProvData[Provider Type]))
return stdValue
So what we do is to calculate the stdev based on the filter given on Code & Type
I'm trying to create a month/year table on power query (M). It seems that has no easy way to do that. Here is the code I created to reach that goal.
Its unclear exactly what you are looking for but
let Years = Table.ExpandListColumn(Table.FromRecords({[Years = {1980..2020}]}), "Years"),
#"Add Months" = Table.ExpandListColumn(Table.AddColumn(Years, "Months", each {1..12}), "Months"),
#"Add Month Names" = Table.AddColumn(#"Add Months", "MonthName", each Date.MonthName(#datetime([Years], [Months], 1,0,0,0)))
in #"Add Month Names"
generates a table like this, and you can change the start/ending year in the code
Here is my code. If anybody could find a easier and more elegant solution, it will be welcomed.
To use this code, create a blanked query, go to Advanced Editor and replace the existing code for this one.
let
first_date = #date(2020, 1, 1),
last_day = DateTime.LocalNow(),
num_months = ((Date.Year(last_day) - Date.Year(first_date)) * 12 + Date.Month(last_day) - Date.Month(first_date)),
list_of_num = List.Numbers(0, num_months, 1),
table_from_list = Table.FromValue(list_of_num, [DefaultColumnName = "Index"]),
add_col_year = Table.AddColumn(table_from_list, "Year", each Date.Year(Date.AddMonths(first_date, [Index])), Int64.Type),
add_col_month = Table.AddColumn(add_col_year, "Mes", each Date.Month(Date.AddMonths(first_date, [Index])), Int64.Type)
in
add_col_month
I'm trying to complete something which should be quite simple but for the life of me, I can't work it out.
I'm trying to calculate the difference between 2 rows that share the same 'Scan type'.
I have attached a photo showing sample data from production. We run a scan and depending on the results of the scan, it's assigned a color.
I want to find the difference in Scan IDs between each Red scan.
Using the attached Photo of Sample data, I would expect a difference of 0 for id 3. A difference of 1 for id 4 and a difference of 10 for id 14.
I have (poorly) written something that works based on the maximum value from the scan id.
I have also tried following a few posts to see if I can get it to work..
var _curid= MAX(table1[scanid])
var _curclueid = MAX(table1[scanid])
var _calc =CALCULATE(SUM(TABLE1[scanid],FILTER(ALLSELECTED(table1[scanid]),table1[scanid]))
return if(_curid-_calc=curid,0,_curid-_calc)
Edit;
Forgot to mention I have checked threads;
57699052
61464745
56703516
57710425
Try the following DAX and if it helps then accept it as the answer.
Create a calculated column that returns the ID where the colour is Red as follows:
Column = IF('Table'[Colour] = "Red", 'Table'[ID])
Create another column as following:
Column 2 =
VAR Colr = 'Table'[Colour]
VAR SCAN = 'Table'[Scan ID]
VAR Prev_ID =
CALCULATE(MAX('Table'[Column 2]),
FILTER('Table', 'Table'[Colour] = Colr && 'Table'[Scan ID] < SCAN))
RETURN
'Table'[Column] - Prev_ID
Output:
EDIT:-
If you want your first value(ID3) to be 0 then relace the RETURN line with the following line:
IF(ISBLANK(Prev_ID) && 'Table'[Colour] = "Red", 0, 'Table'[Column] - Prev_ID )
This will give you the following result:
I was able to find the “2”'s per client with the following formula (Column L).
TotalSimultaneous2 =
IF(Data[Column1]=2,1,0)+
IF(Data[Column2]=2,1,0)+
IF(Data[Column3]=2,1,0)+
IF(Data[Column4]=2,1,0)+
IF(Data[Column5]=2,1,0)+
IF(Data[Column6]=2,1,0)+
IF(Data[Column7]=2,1,0)+
IF(Data[Column8]=2,1,0)+
IF(Data[Column9]=2,1,0)+
IF(Data[Column10]=2,1,0)
Now I need help finding the total amount of columns that contain at least one “2” column N.
In the example below, it would be 7, and that number is coming from the count of all the columns in green since they have at least one “2”.
I can find the simultaneous one. For example, Client4 has the max amount of “2” at the same time, which is 6, but I am having a hard time adding that one “2” from Column10 from Client10 and showing that the number of columns containing a “2” is 7 instead of 6.
Anything helps. Feel free to ask for further clarification, and I will try my best.
You can try this below measure. Here I have added 3 columns but you can add as many as you have-
count_column_with_2 =
CALCULATE(
DISTINCTCOUNT(your_table_name[col1]),
FILTER(your_table_name, your_table_name[col1] = 2)
)
+
CALCULATE(
DISTINCTCOUNT(your_table_name[col2]),
FILTER(your_table_name, your_table_name[col2] = 2)
)
+
CALCULATE(
DISTINCTCOUNT(your_table_name[col3]),
FILTER(your_table_name, your_table_name[col3] = 2)
)
quick question:
I have a total amount that is divided into several classifications, like so:
Total: 7bn
Classification 1: 3bn,
Classification 2: 1bn,
... ,
Classification N: 0,3M
N is such a big number that when I put in a graph, most of the classifications don't even show up in there, so my manager suggested that I took anything that represents less than 5% of the total 7bn and classified them as "Others" to put it all together in the visual.
Then I made a measure "% of total" like:
% of total =
divide(
sum(values),
sumx(
allselected(table),
values
)
)
And this actually works perfect, except...
I wanna make a measure (or calculated column) that returns something like:
new classification =
if(
[% of total] > 0.05,
"Others",
[classification]
)
just to classify for me in the graph
but then only one of the new classifications returns as the old one, the rest returns "Others", but I know there's more than one, according to [% of total].
Can you think of another way to make this work? Is this a dumb question?
Thanks in advance
Create 2 separate measure for [others] & [classification] and create your final measure as below-
new classification =
var is_greater = IF([% of total] > 0.05, 1, 0)
RETURN
SWITCH(
is_greater ,
1,[Others]",
[classification]
)