Get total count of each distinct value - powerbi

If I for example have a column of countries that might repeat and the list follows like this: Spain, Spain, Italy, Spain
I want to get the result that I take the number that a country appears in the column and divide it by total number. I have tried:
CountRows = DIVIDE(DISTINCTCOUNT('Report (7)'[Country]); COUNT('Report (7)'[Country]) )
Any suggestions? do I need a new column for that?

The easiest way to achieve this type of calculation is to add one column with the number of occurrence of the selected words divided by the number of row in the table.
You need to use the function Earlier to get the context.
If you have one table named Table1 and your column Country
Something like :
Divide(COUNTROWS(FILTER(table1, Table1[Country] = EARLIER(Table1[Country]))),COUNTROWS(Table1))
Don't forget to put your new column in Percentage type or add some decimal to see the correct data.

Related

PowerBI: Aggregate Measure correctly by condition on DATEDIFF

I have the following Table:
BaseTable
It represents processes with a certain category.
And there is also a Date Table over column TIMESTAMP.
I would like to show a Measure based on another Measure that calculates the Date-Difference until the selected Date.
So first this is how I calculate the Date-Difference:
AGE =
VAR SELECTED_DATE = CALCULATE(MAX(DATUM[Date]), ALLSELECTED(DATUM))
VAR STARTDATE_PROCESS = Calculate(MAX(Workflow[MIN_TIMESTAMP]),DATUM[Date]<=MAX(DATUM[Date]), ALL(DATUM[Date]))
RETURN
DATEDIFF(STARTDATE_PROCESS,SELECTED_DATE,DAY)
Now I want to use a Measure which depends on the result of AGE, like
NEW = IF([AGE]<=3,CALCULATE(COUNT(Workflow[PROCESS]),DATUM[Date]<=MAX(DATUM[Date]),ALL(DATUM)))
or
OLD = IF([AGE]>3,CALCULATE(COUNT(Workflow[PROCESS]),DATUM[Date]<=MAX(DATUM[Date]),ALL(DATUM)))
The Measures AGE, OLD and NEW look like that with the Base Table:
Measures
As you can see the aggregation is not working correctly:
Result_Wrong
But it should be like that
Result_Correct
Any idea how to fix that?
Thank you!
So the problem is that the subtotal is calculated at a whole different context, and because your Age measure is based on the MAX(Workflow[MIN_TIMESTAMP]) that won't take into account that there can be multiple processes.
To do what you want, you need to change the New and Old measures to perform an aggregation per process and then return the result of that. Something like this:
New_agg =
VAR tbl = ADDCOLUMNS(CALCULATETABLE(VALUES(Workflow[Process]), ALL('Date')), "age", [Age], "count_process", CALCULATE(COUNT(Workflow[Process]), ALL('Date')))
RETURN SUMX(tbl, IF([age]<=3, [count_process]))
Demo File
Let me know if below solution is working
Unfortunately I am unable to generate the dummy data that you have been using, so Created my own data for developing the solution.
Now from this data I have calculated the difference of dates and put it as Age
Now to get the count of process for the condition like yours, I have created two formulas and the result is:
Logic I followed here is, instead of creating measure I have created columns and took the sum of those columns which will give the data you need as sum of those columns.
Column for New:
New = IF((Sheet1[Age]) > 20, 1,0)
Column for Old:
Old = IF((Sheet1[Age]) < 20, 1,0)
Now place both formulas in "Values" and take sum as the aggregation.
Final result is

In the Films table create a calculated column called NumberBreaks which shows for each film the number of breaks needed

The Films table looks like this
There is a ComfortBreaks table looking like image
In the Films table I need to create a calculated column called NumberBreaks which shows for each film the number of breaks needed. To do this I have to pick out the value of the Breaks column where:
The value of the lower limit in the ComfortBreaks table is less than or equal to this film's running time in minutes
and
The value of the upper limit in the ComfortBreaks table is greater than this film's running time in minutes.
the result should look like the image below
There cannot be a relationship between the two tables. so this has to be done without creating relationship between them.
I tried lookup function but it showed error:A table of multiple values was supplied where a single value was expected.
You can use this below code for your custom column. Considering
number_of_breaks =
VAR current_row_run_time_minutes = Films[RunTimeMinutes]
RETURN
MINX (
FILTER(
ComfortBreaks,
ComfortBreaks[LowerLimit] <= current_row_run_time_minutes
&& ComfortBreaks[UperLimit] > MonthNumber
),
ComfortBreaks[Breaks]
)
You can perform your further average calculation on the new custom column now.

Frequency Counter

Good evening everyone, I'm having trouble resolving the following question:
I have "Table 1" containing the occurrence records and "Table 2" containing the occurrences.
I need to set up a VIRTUAL TABLE or VIEW TABLE that presents the occurrences and their frequencies as follows:
Tables with Perspective
The most I could do was bring the occurrences with the total number of rows in the table, ie the same amount X for all.
Last try using DaxStudio
You need to create a measure to show the numbers in the report.
But first, it is recommended to transform the table into a form like below, where each occurrence appears in each row. This has a lot of advantage to make the DAX measure simpler, more accurate, and run faster.
This can be easily achieved using Power Query Editor. The required steps are,
Select the comma delimited occurrences column.
In Transform tab, click Split Column and choose By Delimiter.
Make sure Comma and split at Each occurrence of the delimiter is selected by default.
In Advanced options, select split into Rows, then click OK.
Then you can define a measure to count the number of occurrences, which is as simple as below.
Count = COUNTROWS ( Tabela1 )
Now, in the report area, you can use the measure to obtain the desired output.
I have recreated your tables and here is the DAX for Table3 (perspectiva):
Table3 =
VAR uniqueOccurences = DISTINCT(Table2[Occurence])
RETURN
ADDCOLUMNS(uniqueOccurences,
"count",
VAR currentOccurence = [Occurence]
RETURN
CALCULATE(COUNTA(Tabel1[ID]),
FILTER(Tabel1, CONTAINSSTRING(Tabel1[Occurences], currentOccurence) = TRUE()))
)
This returns the following table:
Please mark this as the solution if this answered your question :)

Power BI remove duplicates based on max value

I have 2 column; ID CODE, value
Remove duplicates function will remove the examples with the higher value and leave the lower one. Is there any way to remove the lower ones? The result I expected was like this.
I've tried Buffer Table function before but it doesn't work. Seems like Buffer Table just works with date-related data (newest-latest).
You could use SUMMARIZE which can be used similar to a SQL query that takes a MIN value for a column, grouped by some other column.
In the example below, MIN([value]) is taken, given a new column name "MinValue", which is grouped by IDCode. This should return the min value for each IDCode.
NewCalculatedTable =
SUMMARIZE(yourTablename, yourTablename[IDCode], "MinValue", MIN(yourTablename[value]) )
Alternatively, if you want the higher values just replace the MIN function with MAX.

Iteratively divide column by column plus suffix (pandas)

In my for loop I would like to divide a column by another. The second column's name is dependent upon the first. Example data frame columns look like this:
column1, column1_mean, column2, column2_mean
I would like to iteratively divide each column by its corresponding mean column ( column1 / column1_mean; column2/column2_mean ).
Thanks for your help.
For a given list of column names cols = ['column1', 'column2', ... ], you can use .div() to divide a list of columns by another list of columns, and use string formatting to make the second list of columns based on the first:
df[cols].div(df[['{0}_mean'.format(col) for col in cols]])
But a simpler way would be to dispense with making the mean columns altogether:
df[cols].div(df[cols].mean())