Calculating dynamic cost from several tables - powerbi

I'm new to Power BI and struggling a bit with concepts. My simplified problem is this:
I have a table with time-track data (possible multiple entries per day for the same user):
datetime
timeTracked (hours)
user
2022-12-01 14:15
1.5
john
2022-12-01 16:30
0.5
john
2022-12-02 12:00
2.5
tom
I also have second table with yearly wages for each user (irregular dates, wage is applicable from that day forward):
date
user
wage
2021-01-01
john
20000
2021-06-15
tom
25000
2022-02-01
john
22000
I want to somehow calculate the cost for a dynamic period or total time, but using other filters (like client).
For example: total cost for current month - the sum of hours for each user multiplied by hourly cost (let's say yearly wage / 1500 for simplicity). The complication is that there might be multiple applicable wages in the selected period.
I'm not sure if I should create a new table, new column(s) to existing table(s), measure(s) or anything else? I don't know how to deal with this problem in Power BI, any hints, links or examples are very appreciated!

Related

power bi dax, filter based on multiple relationships

tldr: I have two tables linked with two relationships, how do I calculate using both?
Hi, I am building a report on a fictional phone company. The data is stored in mongodb with the following format
User Account {
_id : unique account ID
other account details : [...]
yearly records : Array [ Yearly Record]
}
Yearly Record {
year num : ex 2022
monthly records : Array [Monthly Record]
}
Monthly Record {
month num : ex 5 for may
purchased minutes : minutes purchased this month
daily records : Array [Daily Record]
}
Daily Record {
date : date
consumed minutes : minutes used today
}
In this scenario, the minutes do not expire, each month you choose the amount you want to top up. The tables of interest are the monthly and daily tables. PowerBI fuses all user accounts in a single table. I'll omit some things but it looks like:
Monthly Records
_id purchased minutes monthly records idx yearly records idx
account id 100 position in monthly record array same principle
-----------------------------------------------
Daily Records
_id used minutes date daily records idx monthly records idx yearly records idx
account id 5 2022-10-31 ... ... ...
All tables are linked using _id in the modeling. For daily and monthly, the active relationship is the month idx. But since the tables are in the db, the month idx repeat so the relationship is many to many.
I havent found a way to filter out only the monthly details for only one user. SO if I were to put id and monthly purchase in a table, each user contains the reords of all users (see below)
Monthly Records
_id purchased minutes monthly records idx yearly records idx
1 100 0 0
2 200 0 0
Table:
id year month purchased min
1 2022 jan 100
1 2022 jan 200
2 2022 jan 100
2 2022 jan 200
The right months line up but the records are treated as duplicates (so if in feb 1 bought 300, we'd see feb 300 for both 1 anbd 2). I would almost need two active relationships on both the month idx and the id
How do I create a measure that is affected by both relationships? For instance if I chose id =1 and year + month = jan 2022 in slicers I should get only 100
Is this even possible in dax? I tried playing around with filter by id to no avail. In python there would be ways but then we deal with script visuals
My end goal is graphing minutes balance over time with
measure = bought - consumed
we'd see spikes at beginnings of month and the nline slowly go down as muinutes are used
I know you cannot outright define two active relationships but are there ways to deal with data as such?

Power BI Finding Amount Due from Customer Transactions

I have a table called CustTrans, with the following columns:
VOUCHER , TRANSDATE , AMOUNT , RECID , CUSTOMERID , DUEDATE
When an invoice is issued or payment is posted, a new line is created.
The connection between invoice and payment is created in a separate table called CustSettlement. In this table you can find SETTLEDAMOUNT with the TRANSDATE for settlement, and a TRANSRECID which is the same as the RECID of the voucher that has been settled.
I have created two measures to get Total Account Receivables today and over time:
Total Account Receivables = SUM(CustTrans[Amount])
And a measure to get Accrued Account Receivables over time:
Accrued AR = CALCULATE([Total Account Receivables], FILTER(ALL(Dates[DATE]), Dates[DATE]<=MAX(Dates[DATE])))
With the last measure I can see the development over time.
I have created a new table to get all dates: Dates = CALENDAR(MINA(CustTrans[TRANSDATE]), MAXA(CustTrans[TRANSDATE]))
What I am struggling with now is to determine the following:
Overdue amount 1-14 days, overdue amount 15-28 days and overdue amount over 28 days.
I can show the situation today by calculated column:
AMOUNTREMAINDER = AMOUNT - SETTLEDAMOUNT with RECID=TRANSRECID and determining the overdue status for every transaction with remaining amount.
However, I also need to show the development over time for each of the three categories, and I am struggling to combine the TRANSDATE in CustTrans and the TRANSDATE in CustSettlement to create an accrued measure.
Please let me know if I need to submit additional information.

Need to find total sales of each date

I'm stuck in finding total sales of a day, I need to further find out the % of each product in a particular date.
for eg: sales of beverages for 2/17/2018
Here I am trying to find the total sales for one particular date :
I am expecting 922 in the salesperday column.
your table:
Transaction_Date
product_group
sales
SalesPerday
2/17/2018
Add-ons
$11.2
$11.2
2/17/2018
Beverages
$725.25
$725.25
2/17/2018
Food
$133.25
$133.25
2/17/2018
Merchandise
$24
$24
2/17/2018
Whole Bean/Teas
$28.95
$28.95
2/18/2018
Add-ons
$27.2
$27.2
2/18/2018
Beverages
$817.3
$817.3
2/18/2018
Food
$138
$138
2/18/2018
Merchandise
$54
$54
2/18/2018
Whole Bean/Teas
$178.18
$178.18
you need to add Calculated Columns for day with following formula:
Day = DAY('table'[Transaction_Date])
then you need SalePerDay measure
SalePerDay = CALCULATE(SUM('Table'[sales]),ALLEXCEPT('Table','Table'[Day]))
at last create following measure for calculate percentage of product category per day
%Sale = SUM('Table'[sales])/[SalePerDay]
just aware of implicit measure and don't use them.

Power BI Measure - Checkbook balance column

Objective:
Looking to generate a column measure (DYNAMIC BALANCE) which reads the first row of BALANCE, and calculate the balance from subsequent rows using AMOUNT column.
Problems encountered:
Measure should ignore the AMOUNT on the first row and only capture the BALANCE into a variable.
My first row formula grabs both values for 1/1/2021 rather than only the top most row balance value.
Date
Description
AMOUNT
BALANCE
1/1/2021
Dog Food
-20.00
980.00
1/1/2021
McDonalds
-30.00
950.00
1/5/2021
Pay Day
1000.00
1950.00
1/8/2021
Dog Food
-20.00
1930.00
1/10/2021
Medical
-1000.00
930.00
1/18/2021
McDonalds
-30.00
900.00
1/21/2021
Pay Day
1000.00
1900.00
1/31/2021
Dog Food
-50.00
1850.00
2/2/2021
McDonalds
-40.00
1810.00
You are my hero!
Here is the solution. Funny how the solution is so simple in DAX and in hindsight. M-Code would have been more complicated. DAX is dynamic to the filter which is necessary.
New Measure:
RunningTotal = FIRSTNONBLANK(bankdownload[Balance],0)+SUM(bankdownload[Amount])-FIRSTNONBLANK(bankdownload[Amount],0)

Pandas - calculating interaction between buyers and sellers

So I have a programming problem, which I am finding a little challenging. I have daily data for several years (located in Pandas DataFrame) on buyers and sellers ala. this:
Seller Buyer Amount
2012/11/13 Bank1 Bank2 15
2012/11/13 Bank1 Bank2 17
2012/11/13 Bank5 Bank3 5
2012/11/14 Bank4 Bank2 10
2012/11/14 Bank1 Bank3 22
Index is Pandas.DatetimeIndex.
I would like to calculate, on a month-level basis for each buying bank, what share of their total monthly volume comes from each seller, they have interacted with. So in the above case the output (preferably a DataFrame as well) would be:
Month Seller Buyer Share
2012/11 Bank1 Bank2 32/42
2012/11 Bank4 Bank2 10/42
2012/11 Bank5 Bank3 5/27
2012/11 Bank1 Bank3 22/27
Any input is greatly appreciated!
I have uploaded an IPython notebook to Github that should begin to answer your question. You can find it here.
The basic approach here is to do DataFrame.groupby() operations. Using the dummy data you provided, I first did data.groupby('Buyer').sum(), reset the index to the buyer, add back the sum as a "total" column, and finally divide.
I am not so sure on how to do the timeseries stuff (i.e. grouping by month when you only have full date info not split into year month date). If others have suggestions, please leave a comment so I can update the notebook and my answer!