Remaining working days in a date interval - powerbi

Suppose that i have a table, in which I want to make a column where I count the remaining workdays in a date interval with respect to the current date. An example of such may look like this:
Id Date Start End Remaining
+---+------------+------------+------------+-----+
| 1 | 01-01-2020 | 01-01-2020 | 31-12-2020 | 262 |
+---+------------+------------+------------+-----+
| 1 | 02-01-2020 | 01-01-2020 | 31-12-2020 | 261 |
+---+------------+------------+------------+-----+
| 1 | 03-01-2020 | 01-01-2020 | 31-12-2020 | 260 |
+---+------------+------------+------------+-----+
| 1 | 04-01-2020 | 01-01-2020 | 31-12-2020 | 260 | <--- Weekend
+---+------------+------------+------------+-----+
| 1 | 05-01-2020 | 01-01-2020 | 31-12-2020 | 260 | <--- Weekend
+---+------------+------------+------------+-----+
| 1 | 06-01-2020 | 01-01-2020 | 31-12-2020 | 259 |
+---+------------+------------+------------+-----+
| 1 | 07-01-2020 | 01-01-2020 | 31-12-2020 | 258 |
+---+------------+------------+------------+-----+
Here, I have a date column and the respective start/end dates. As you can see, optimally, I want the countdown to stop, whenever I hit a weekend, similar to the above example.
Additionally, I want to do it in a way, so that I respect individual Ids... That is, I have different Ids with different start and end dates, further down the table.
Currently, I have a hard time figuring out how to make the countdown stop. As of now, I have tried the following DAX formula:
Sum_without_weekends = CALCULATE(COUNTROWS('Table'),
FILTER('Table'(SWITCH(WEEKDAY([Date]),1,0,7,0,1) <> 0) && ('Table'[Id]=EARLIER('Table'[Id]))))
+ IF((SWITCH(WEEKDAY('Table'[Date]),1,0,7,0,1) <> 0),DATEDIFF('Table'[Date],'Table'[Start].[Date],DAY), 0)
This does however yield something along the lines of this, which is wrong:
Id Date StartDate EndDate Remaining
+---+------------+------------+------------+-----+
| 1 | 01-01-2020 | 01-01-2020 | 31-12-2020 | 262 |
+---+------------+------------+------------+-----+
| 1 | 02-01-2020 | 01-01-2020 | 31-12-2020 | 261 |
+---+------------+------------+------------+-----+
| 1 | 03-01-2020 | 01-01-2020 | 31-12-2020 | 260 |
+---+------------+------------+------------+-----+
| 1 | 04-01-2020 | 01-01-2020 | 31-12-2020 | 262 | <--- Weekend
+---+------------+------------+------------+-----+
| 1 | 05-01-2020 | 01-01-2020 | 31-12-2020 | 262 | <--- Weekend
+---+------------+------------+------------+-----+
| 1 | 06-01-2020 | 01-01-2020 | 31-12-2020 | 257 |
+---+------------+------------+------------+-----+
| 1 | 07-01-2020 | 01-01-2020 | 31-12-2020 | 256 |
+---+------------+------------+------------+-----+
Therefore, I need help finishing the DAX formula, in order to make the countdown stop when I hit a weekend, similar to what is visualized in the first table.

Perhaps something like this:
Remaining Working Days =
VAR ID1 = Table1[ID]
VAR Date1 = Table1[Date]
VAR Work_Days = CALCULATE(COUNTROWS(Table1),FILTER(Table1,Table1[ID]=ID1),FILTER(Table1,WEEKDAY(Table1[Date]) in {2,3,4,5,6}),FILTER(Table1,Table1[Date]>=Date1))
RETURN Work_Days
The above calculation uses just the "Date" variable to get the desired result. The idea is to count all working days after the current date and applying a filter for ID as well. Hope this helps.

Related

Opening Stock Value In Powerbi

I am working on Cost of goods sold using Powerbi.
https://www.mediafire.com/file/pmb7u1thsag1kq1/Cost+of+goods+sold.pbix/file
Above is my file which i uploaded on mediafire.
i am taking the average price by using average function year wise.
If you see in File.
| GName | Year | Opening Stock | InQty | OutQty | InItemValue | Average Value | Closing Stock | Closing Stock Value | Opening Stock Value | Cost of Goods Sold |
|-------------|------|---------------|-------|--------|-------------|---------------|---------------|---------------------|---------------------|--------------------|
| Bahria Town | 2016 | | 4454 | 3586 | 126610299.8 | 28426.20113 | 868 | 24673942.58 | 0 | 101936357.2 |
| Bahria Town | 2017 | 868 | 6379 | 6547 | 166903971.5 | 23030.76743 | 700 | 16121537.2 | 0 | 150782434.3 |
| Bahria Town | 2018 | 700 | 9129 | 8709 | 271932546.3 | 27666.3492 | 1120 | 30986311.11 | 0 | 240946235.2 |
| Bahria Town | 2019 | 1120 | 9333 | 9393 | 313226466.8 | 29965.22212 | 1060 | 31763135.45 | 0 | 281463331.4 |
| Bahria Town | 2020 | 1060 | 10192 | 10136 | 362950101.2 | 32256.49673 | 1116 | 35998250.35 | 0 | 326951850.8 |
| Bahria Town | 2021 | 987 | 8882 | 8468 | 404199067.4 | 40956.43605 | 1530 | 62663347.16 | 0 | 346819100.5 |
In Above as you can see i just took the Average Value
Average Value = ([Opening Stock Value]+[initemvaluee])/([inqtyy]+[Opening Stock])
and closing stock value
Closing Stock Value = [Average Value] * [Closing Stock]
When i calculate the closing stock value of previous year its give me error.
circular dependency was detected: Measure: 'mak_stockInHandValue'[Average Value], Measure: 'mak_stockInHandValue'[Opening Stock Value], Measure: 'mak_stockInHandValue'[Average Value].
Any Suggestion to see the closing stock value in the field of opening stock value ?????
I am working on this from more than 2 weeks.
Please help me out
Thanks in advance

Power Bi, Dax - calculations, filters, balance

Could you please help me to solve the problem as I am totally new to DAX and English is not my first language so I am struggling to even find the correct question.
Here's the problem.
I have two tables:
start_balance
+------+---------------+
| Type | Start balance |
+------+---------------+
| A | 0 |
| B | 10 |
+------+---------------+
in_out
+------+-------+------+----+-----+
| Year | Month | Type | In | Out |
+------+-------+------+----+-----+
| 2020 | 1 | A | 20 | 20 |
| 2020 | 1 | A | 0 | 10 |
| 2020 | 2 | B | 20 | 0 |
| 2020 | 2 | B | 20 | 10 |
+------+-------+------+----+-----+
I'd like to get the result as follows:
Unfiltered:
+------+-------+------+---------+----+-----+------+
| Year | Month | Type | Balance | In | Out | Left |
+------+-------+------+---------+----+-----+------+
| 2020 | 1 | A | 0 | 20 | 20 | 0 |
| 2020 | 1 | B | 10 | 20 | 10 | 20 |
| 2020 | 2 | A | 0 | 20 | 10 | 10 |
| 2020 | 2 | B | 20 | 20 | 10 | 30 |
+------+-------+------+---------+----+-----+------+
Filtered (for example year/month 2020/2):
+------+-------+------+---------+----+-----+------+
| Year | Month | Type | Balance | In | Out | Left |
+------+-------+------+---------+----+-----+------+
| 2020 | 2 | A | 0 | 20 | 10 | 10 |
| 2020 | 2 | B | 20 | 20 | 10 | 30 |
+------+-------+------+---------+----+-----+------+
So while selecting a slicer for the year/month it should calculate balance before selected year/month and then show selected year/month values.
Edit: corrected start_balance table.
Is the sample data correct?
A -> the starting balance is 10, but in your unfiltered table example, it is 0.
Do you have any relationship between these tables?
Does opening balance always apply to the current year? What if 2021 appears in the in_out table? How do you know when the start balance started?
example without starting balance
If you want to show value breaking given filter you should use statement ALL or REMOVEFILTERS function (in Analysis Services 2019 and in Power BI since October 2019).
calculate(sum([in]) - sum([out]), all('in_out'[Year],'in_out'[Month]))
More helpful information:
https://www.sqlbi.com/articles/managing-all-functions-in-dax-all-allselected-allnoblankrow-allexcept/

DAX measure with month variable based on date field

I am having a hard time getting the following measure to work. I am trying to change the target based on a date filter. My filter is the Workday columns, where Workday is a standard date column. sMonth is a month columns formatted as whole number. I am looking to keep the slicer granular, in order to work by day, adding custom columns with month and year and basing the measure on those would help. This is what I have tried and couldn't get it to work:
Cars Inspected =
VAR
selectedMonth = MONTH(SELECTEDVALUE('All Cars Inspected'[Workday]))
RETURN CALCULATE(SUM(Targets[Target]),
FILTER(Targets,Targets[Location]="Texas"),
FILTER(Targets,Targets[Description]="CarsInspected"),
FILTER(Targets,Targets[sMonth]=selectedMonth))
I would appreciate if someone would suggest a different way of achieving the same result.
LE:
This is a mock-up of what I am trying to achieve:
The total cars get filtered by the Workday. I would like to make the Targets/Ranges dynamic. When the slider gets adjusted everything else is adjusted.
My tables look like this:
+-----------+--------------------+----------+
| Workday | TotalCarsInspected | Location |
+-----------+--------------------+----------+
| 4/4/2017 | 1 | Texas |
| 4/11/2017 | 149 | Texas |
| 4/12/2017 | 129 | Texas |
| 4/13/2017 | 201 | Texas |
| 4/14/2017 | 4 | Texas |
| 4/15/2017 | 6 | Texas |
+-----------+--------------------+----------+
+----------+--------+----------+---------------+--------+-----+--------+
| TargetID | sMonth | Location | Description | Target | Red | Yellow |
+----------+--------+----------+---------------+--------+-----+--------+
| 495 | 1 | Texas | CarsInspected | 3636 | 0.5 | 0.75 |
| 496 | 2 | Texas | CarsInspected | 4148 | 0.5 | 0.75 |
| 497 | 3 | Texas | CarsInspected | 4861 | 0.5 | 0.75 |
| 498 | 4 | Texas | CarsInspected | 4938 | 0.5 | 0.75 |
| 499 | 5 | Texas | CarsInspected | 5094 | 0.5 | 0.75 |
| 500 | 6 | Texas | CarsInspected | 5044 | 0.5 | 0.75 |
| 501 | 7 | Texas | CarsInspected | 5043 | 0.5 | 0.75 |
| 502 | 8 | Texas | CarsInspected | 4229 | 0.5 | 0.75 |
| 503 | 9 | Texas | CarsInspected | 4311 | 0.5 | 0.75 |
| 504 | 10 | Texas | CarsInspected | 4152 | 0.5 | 0.75 |
| 505 | 11 | Texas | CarsInspected | 3592 | 0.5 | 0.75 |
| 506 | 12 | Texas | CarsInspected | 3748 | 0.5 | 0.75 |
+----------+--------+----------+---------------+--------+-----+--------+
Let the Value for your gauge be the sum of TotalCarsInspected and set the Maximum value to the following measure:
Cars Inspected =
VAR selectedMonth = MONTH(MAX('All Cars Inspected'[Workday]))
RETURN LOOKUPVALUE(Targets[Target],
Targets[Location], "Texas",
Targets[Description], "CarsInspected",
Targets[sMonth], selectedMonth)

Pandas: consecutive rows' value change comparison

I have a Dataframe with date as index:
Index | Opp id | Pipeline_Type |Amount
20170104 | 1 | Null | 10
20170104 | 2 | Sou | 20
20170104 | 3 | Inf | 25
20170118 | 1 | Inf | 12
20170118 | 2 | Null | 27
20170118 | 3 | Inf | 25
Now I want to calculate number of records(Opp id) for which Pipeline type has changed or amount has changed (+/-diff). Above no of records will be 2 for pipeline_type as well as for amount.
Please help me frame the solution.

django - query - most recent entry by user

here is my model
class gameUserTrophyInfo(models.Model):
user = models.ForeignKey(userInfo)
trophy = models.ForeignKey(gameTrophyInfo)
date = models.DateTimeField()
i want a query that returns to me: for all users, return the most recent trophy.
Example:
my db:
date (Day - Month - Year )
| user | trophy | date |
| 1 | 1 | 10-10-10 10:00:00 |
| 1 | 2 | 10-10-10 09:00:00 |
| 2 | 1 | 10-10-10 01:00:00 |
| 2 | 2 | 11-10-10 01:00:00 |
| 3 | 10 | 20-10-10 01:00:00 |
returns:
| user | trophy | date |
| 1 | 1 | 10-10-10 10:00:00 |
| 2 | 2 | 11-10-10 01:00:00 |
| 3 | 10 | 20-10-10 01:00:00 |
how can i do that ?
thanks !
You need two queries:
users = User.objects.annotate(max_trophy_info_id=Max('gameusertrophyinfo__id'))
ids = [user.max_trophy_info_id for user in users]
trophy_infos = gameUserTrophyInfo.objects.filter(id__in = ids)