How to create month table in powerquery (M) - powerbi

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

Related

Power BI calculate increase based on next & previous values

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

Calculate the difference between 2 rows in PowerBI using DAX

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:

if else and while in M language

There are 165 Values here and they are Comma Separated. This step is called CommaSeperated
workItemList is a function which takes in the value from CommaSeperated and Brings out the table here
I want to split the 165 items in CommaSeparated into batches of 100 and call workItemList for each batch.
Any ideas on how it must be done?
i have managed to take a comma separated text string and turn it into a grouped table of 100's.
first 100 is called "100" next 100 is called "200" and starts at 101.
enter image description here
here are my steps
enter image description here
First i split the text string into columns using comma as the separator tag, using the for each option.
Then i transposed the whole thing into one column.
Add an index.
Add a modulus of 100
Add a custom column "100counter": = Table.AddColumn(#"Inserted Modulo", "100counter", each if [Modulus]=99 then [Indeks]+1 else null)
forget the renamed columns step :D
used Fill up first, because the to initial 0-100 will have "null" as their modulus.
used Fill down second, because the last records can be null if it doesnt end exactly on 99.
Grouped by "100counter", all rows.
Abbridged code - i cleaned up the 1700 columns in the split by delimiter step :
let
Source = Excel.CurrentWorkbook(){[Name="Tabel2"]}[Content],
#"Split Column by Delimiter" = //Alot of spam code here which is essentially just alll the columns being split. Mark ALL, choose split columns by delimiter, choose comma and all.
#"Transposed Table" = Table.Transpose(#"Split Column by Delimiter"),
#"Added Index" = Table.AddIndexColumn(#"Transposed Table", "Indeks", 0, 1),
#"Inserted Modulo" = Table.AddColumn(#"Added Index", "Modulus", each Number.Mod([Indeks], 100), type number),
#"Added Custom" = Table.AddColumn(#"Inserted Modulo", "100counter", each if [Modulus]=99 then [Indeks]+1 else null),
#"Filled Up" = Table.FillUp(#"Added Custom",{"100counter"}),
#"Filled Down" = Table.FillDown(#"Filled Up",{"100counter"}),
#"Grouped Rows" = Table.Group(#"Filled Down", {"100counter"}, {{"Antal", each _, type table [Column1=number, Indeks=number, Modulus=number, 100counter=number]}})
in
#"Grouped Rows"

DAX function that check if value is in range and return corresponding value in other table

I have two tables: a "Range" table that has the ranges and its corresponding values and a "Data" table that has the values I want to check against the ranges.
My Data table is as follows, but it has more rows than the Range table. The "PM" values are the ones that I need to check.
Location PM
B01 1,05
B02 1,04888
B15 1,05787
B16 1,05787
B03 2,03714
B04 2,03714
B09 2,03714
B10 2,03714
B17 2,03714
And the "Range" table is like this sample:
P.E P.S PV
0,48 1,03 10,00%
1,03 2,02 10,03%
2,02 3,63 8,87%
3,63 6,23 8,24%
6,23 10,17 7,62%
10,17 15,79 6,46%
15,79 22,37 5,75%
22,37 30,70 5,29%
30,70 41,27 4,99%
41,27 54,88 4,86%
54,88 71,57 4,65%
So, summing up, I need to create a DAX measure or column that checks if the PM value is between the PE and PS values and return the corresponding PV.
On excel, I managed to do this usind the LOOKUP function, as this function rounds the searched value to the nearest smaller value in the corresponding table to give a match. On Power Bi I coudn't find a way to replicate this.
Does someone knows if it's possible?
Thanks for all the help!
You need a measure as below-
respected_pv =
CALCULATE(
MAX(range[PV]),
FILTER(
ALL(range),
range[P.E] <= MIN(data[PM])
&& range[P.S] >= MIN(data[PM])
)
)
Here is the code for a Custom Column-
respected_pv_column =
CALCULATE(
MAX(range[PV]),
FILTER(
ALL(range),
range[P.E] <= data[PM]
&& range[P.S] >= data[PM]
)
)
Here below is the sample output. Remember, only first row get the PV as other ranges are not available in your sample data.

Filter and compare specific data (Power BI/DAX)

I am trying to do an If formula in Power bi, with filtering and comparing data. I want to check for every Client,who have with unique Transaction ID, if the Legal firm is the same. If its the same to return Yes, if not - NO.
**Client | Transaction ID | Legal firm**
American Express |2295876 |Orrick Herrington
American Express |2295877 |Orrick Herrington
American Express |2295878 |Orrick Herrington
Swedbank AB |2287074 |Linklaters
Swedbank AB |2287074 |Clifford Chance
Swedbank AB |2287075 |Clifford Chance
I tried Calculate with distinct count, but it wasn't possible to include if statement.
You should be able to do it with COUNT, and removing the filter context on the Legal Firm using ALLEXCEPT, for example
Measure =
VAR rowCheck = CALCULATE(COUNT(Table1[Legal firm]), ALLEXCEPT(Table1, Table1[Transaction ID]))
VAR textValue = IF(rowCheck = 1, "Yes", "No")
RETURN
textValue
[
Hope that helps