PowerBI | Categorise revenues as new customers in a new column - powerbi

I'm hoping you all have a great day and maybe someone can make mine great too.
sampleData:
date; opportunity; customerID; revenue
01.01.2001; a; A; 50
01.01.2001; b; A; 50
03.04.2001; c; A; 50
05.05.2001; d; B; 150
26.10.2001; e; A; 100
I wanted to add a new calculated column with DAX which says if an revenue entry is either from a new customer or from an existing one. My thinking was that a new-customer-revenue is defined as all revenues of the first day an customer bought something. Which makes row 1, 2 and 4 of the sample data table above new-customer-revenues.
My thoughts somehow where:
new customer =
VAR var_Customer = 'sampleData'[customerID]
VAR var_SubTable = FILTER('sampleData', 'sampleData'[customerID] = var_Customer)
VAR var_firstDate = MIN(var_SubTable[date])
RETURN
IF('sampleData'[date] = var_firstDate, "New Customer", "Existing Customer")
But the problem is that MIN() needs a column and I cant return a single column from var_SubTable... I also didn't find other suitable solutions for the problem. For me it would be really nice to have a new column, but if theres only a solution by using a measure that's also interesting to hear...
Thanks!
Hisager

You are almost there.
Try using the following DAX Formula:
new customer =
VAR __Customer = 'sampleData'[customerID]
VAR __SubTable = FILTER('sampleData', 'sampleData'[customerID] = __Customer)
VAR __FirstDate = CALCULATE(MIN(sampleData[date]), __SubTable)
RETURN
IF('sampleData'[date] = __FirstDate, "New Customer", "Existing Customer")
It's basically the same but using calculate to get the min date.

Related

power bi DAX hierarchical table concatenation of names

Since two days I'm on a problem and I can't solve it so I come here to ask some help...
I have that bit of dax that basically take the path of a hierarchical table (integers) and take the string names of the 2 first in the path.
the names I use:
'HIERARCHY' the hierarchical table with names, id, path, nbrItems, string
mytable / addedcolumn1/2 the new table used to emulate the for loop
DisplayPath =
var __Path =PATH(ParentChild[id], ParentChild[parent_id])
var __P1 = PATHITEM(__Path,1) var __P2 = PATHITEM(__Path,2)
var l1 = LOOKUPVALUE(ParentChild[Place],ParentChild[id],VALUE(__P1))
var l2a = LOOKUPVALUE(ParentChild[Place],ParentChild[id],VALUE(__P2))
var l2 = if(ISBLANK(l2a), "", " -> " & l2a)
return CONCATENATE(l1,l2)
My problem is... I don't know the number of indexes in my path, can go from 0 to I guess 15...
I've tried some things but can't figure out a solution.
First I added a new column called nbrItems which calculate the number of items in the list of the path.
The two columns:
Then I added that bit of code that emulates a for loop depending on the number of items in the path list, and I'd like in it to
get name of parameters
concatenate them in one string that I can return and get
string =
var n = 'HIERARCHY'[nbrItems]
var mytable = GENERATESERIES(1, n)
var addedcolumn1 = ADDCOLUMNS(mytable, "nom", /* missing part: get name */)
var addedcolumn2 = ADDCOLUMNS(addedcolumn1, "string", /* missing part: concatenate previous concatenated and new name */)
var mymax = MAXX(addedcolumn2, [Value])
RETURN MAXX(FILTER(addedcolumn2, [Value] = mymax), [string])
Full table:
Thanks for your help in advance!
Ok, so after some research and a lot of try and error... I've came up to a nice and simple solution:
The original problem was that I had a hierarchical table ,but with all data in the same table.
like so
What I did was, adding a new "parent" column with this dax:
parent =
var a = 'HIERARCHY'[id_parent]
var b = CALCULATE(MIN('HIERARCHY'[libelle]), FILTER(ALL('HIERARCHY'), 'HIERARCHY'[id_h] = a))
RETURN b
This gets the parent name from the id_parent (ref. screen).
then I could just use the path function, not on the id's but on the names... like so:
path = PATH('HIERARCHY'[libelle], 'HIERARCHY'[parent])
It made the problem easy because I didn't need to replace the id's by there names after this...
and finally to make it look nice, I used some substitution to remove the pipes:
formated_path = SUBSTITUTE('HIERARCHY'[path], "|", " -> ")
final result

LOOP to Look up the value in M Query

I have a following data table:
I need to create a column called FINAL VALUE with certain rules in the power query editor:
The FINAL VALUE is created based on the COUNTRY
If VALUE is greater than 100, then the previous value will be taken
if the previous values (no previous value such as 202001) are all greater than 100, then 100
So the final table should look like:
I sort COUNTRY and DATE, and then I try to apply loop in M Query. As I am not familiar with the function in M query, I have hard time figuring out. I appreciate any help. Thank you.
I believe you are better of in DAX. You can create a new column like below:
Final Value =
var curCountry = TableCountry[Country]
var curDate = TableCountry[Date]
var prevDate = CALCULATE(MAX(TableCountry[Date]), FILTER(TableCountry, curDate >= TableCountry[Date] && curCountry = TableCountry[Country] && TableCountry[Value] <=100))
return LOOKUPVALUE(TableCountry[Value], TableCountry[Country], curCountry, TableCountry[Date], prevDate, 100)

How do I create calculated column in power bi?

I am new to Power BI and was wondering if someone can help me.
I have a table
A B Status
---------------------------
Asset1 B1 Compliant
Asset1 B2 N/A
Asset2 B1 Non- Compliant
Asset2 B2 Compliant
Asset3 B1 Compliant
Asset3 B2 Compliant
I have to find the Asset which are 100% compliant(Count of Asset where status of all the rows of Column B is Compliant and not non Compliant and we need to ignore N/A.
Out put I want
A 100%Compliant
Asset1 Y
Asset2 N
Asset3 Y
Thanks
You can create an extra column in your table:
100% Cpmpliant =
var asset = Compliant[A]
var allRows = CALCULATE(COUNTROWS(Compliant); FILTER(Compliant; Compliant[A] = asset && Compliant[Status] <> "N/A"))
var compliantRows = CALCULATE(COUNTROWS(Compliant); FILTER(Compliant; Compliant[A] = asset && Compliant[Status] = "Compliant"))
return if (allRows = compliantRows; "Y";"N")
This calculates the rows of the asset without the N/A and compares it against teh rowcount of the compliant. If this is equal, they are all compliant-
Next you can create a visual with two columns ass below:
Edited:
You can create a measure:
IsCompliant =
VAR NumNonCompliant = CALCULATE(COUNTAX('Table', [B]), 'Table'[Status] = "Non - Compliant")
RETURN IF(NumNonCompliant = 0, "Y", "N")
The variable NumConCompliant calculates the number of rows with status = "Non - Compliant". Then it is compared with 0 to find any non-compliance.
Then the measure returns the state of "A" in the given context. This gives you the flexibility of assigning compliance measure to a combination of assets. For example if you filter data on "A = Asset1", the result would be "Y", and (A = Asset1 || A = Asset2) results in a "N". (Since the combination of Asset1 & Asset2 are non-compliant).
Refer to this doc about measures and contexts.

Google Sheets - formula for expense sheet

I need a formula that will help me calculate the $ expense amount for my milage rates.
Here is my sheet https://docs.google.com/spreadsheets/d/1PPFuFWbxWi9iIdtYBJvnSNB1j1cjjQutepF3hnpGoFM/edit?usp=sharing
On the tab that's expenses, I need to check the year of my date in Col A (because different years have different rates), then check Col D to see if it says Milage, then check Col E for what type of milage (there are 4 types), then return the number input in Col I times the appropriate rate for that year and type of milage. I have a table set up with the rates in another tab.
I'm thinking it will be a long IF and AND formula. Any help would be great!
In 'Essentials Log', I've changed the format of your year start-end dates to state the year you're referencing. I've added a column for "Milage".
Expense Reference Screenshot
In 'Expenses'!M2, I tried this and it seems to work:
=ARRAYFORMULA(iferror(if(isblank(A2:A),"",I2:I*vlookup(D2:D&year(A2:A)&E2:E,query({'Essentials Log'!Y2:Y&'Essentials Log'!Z2:Z&'Essentials Log'!AA2:AA,'Essentials Log'!AB2:AB},"Select *"),2,false))))
Working Example Screenshot
Have a look (I copied your example sheet): Milage expense
I solved this with creating an app script.
I realized I didn't want to use a formula because in my $ col most entries will be keyed in and so I didn't want to copy and paste a formula every time.
Here is the code if anyone might need it. I added it to the menu so the calculation is done with a click.
function expenseMilageCalculation() {
var auditionsSheet = SpreadsheetApp.getActiveSpreadsheet();
var expensesTab = auditionsSheet.getActiveSheet();
var activeCell = expensesTab.getActiveCell();
var activeRow = activeCell.getRow();
var expenseDate = expensesTab.getRange(activeRow, 1).getValue();
var expenseYear = expenseDate.getFullYear();
var expenseType = expensesTab.getRange(activeRow, 4).getValue();
var expenseDetails = expensesTab.getRange(activeRow, 5).getValue();
var numMiles = expensesTab.getRange(activeRow, 9).getValue();
var essentialsLogTab = auditionsSheet.getSheetByName("Essentials Log")
//2018 Business Milage
if (expenseYear == 2018 && expenseType == "Milage" && expenseDetails == "Business") {
var milageBusinessRate2018 = essentialsLogTab.getRange(3, 27).getValue();
var dollarMilesDeduction = numMiles * milageBusinessRate2018
expensesTab.getRange(activeRow, 8).setValue(dollarMilesDeduction)
}
//2019 Business Milage
if (expenseYear == 2019 && expenseType == "Milage" && expenseDetails == "Business") {
var milageBusinessRate2019 = essentialsLogTab.getRange(8, 27).getValue();
var dollarMilesDeduction = numMiles * milageBusinessRate2019
expensesTab.getRange(activeRow, 8).setValue(dollarMilesDeduction)
}

Database table not being updated

I have been struggling with this piece of code. Everything is being updated except my items table in the database. Need the sales part to be plus 1 each time someone makes a purchase.
$setQuery = '';
if($extended) {
$setQuery = " `status` = 'extended_buy', ";
}
$mysql->query("
UPDATE `items`
SET `sales` = `sales` + 1,
$setQuery
`earning` = `earning` + '".sql_quote($price)."'
WHERE `id` = '".intval($item['id'])."'
");
return true;
}
You will need to use Subquery for the same
It would be somewhat like this
SET sales = (From items where WHERE id = '".intval($item['id']) + 1,
You will need to pull its value and then Add it.
In other case you can pick it in variable and update it.