DAX or Excel query - excel

How can I write a query in DAX so if I have a data like this, where there is startdate and end date and the total working hour and I need to assign the working hours values to each day .
So what I want is when the start date and the end date is not the same date than the Hours divide between those days.
For example -
User Start-Date End-DATE Hour
Dan 2015-02-05 2015-02-08 32
Here the Start-Date is feb 05 and the End-DATE is 2015-02-08 and Hour is 32
The difference between Start-Date and End-DATE is 4 days.
So I want to divide the hours by the difference of Start-Date and End-DATE and assign those values on each days
So the Expected Output will be ---
User Date Hour
Dan 2015-02-05 8
Dan 2015-02-06 8
Dan 2015-02-07 8
Dan 2015-02-08 8
What I have
User Start-Date End-DATE Hour
Dan 2015-02-05 2015-02-08 32
Dan 2015-02-09 2015-02-09 6
Dan 2015-02-10 2015-02-11 3
Dan 2015-02-11 2015-02-12 8
Expected result -
User Date Hour
Dan 2015-02-05 8
Dan 2015-02-06 8
Dan 2015-02-07 8
Dan 2015-02-08 8
Dan 2015-02-09 6
Dan 2015-02-10 3
Dan 2015-02-11 8
Any one have an idea how to do that in DAX or excel query !

So just before I start I have used Power Bi for this solution which is a free tool to use for personal use however there are some licensing options available. Essentially the premise is the same however there is an element of Power Query used along with some DAX. Anyway here are my steps:
Here is my table before I have made any changes
User Start End Hour
UserA 01/01/2018 05/01/2018 32
Create a custom column to calculate the number of dates between [Start] and [End]
Dates = { Number.From([Start]..Number.From([End]) }
Create another custom column to calculate the number of [Hour]s divided by the number of list items. Do not expand the list before this step!
CountPerDay = [Hour] / List.Count([Dates])
Finally expand your list column to show the number of hours required per day. Note how the dates are in a numeric format. Changing the column data type to "Date" or "DateTime" will change these back to the correct value.
Using my example you should now have a table that looks something like the below:
User Start End Hour Dates CountPerDay
UserA 01/01/2018 05/01/2018 32 01/01/2018 6.4
UserA 01/01/2018 05/01/2018 32 02/01/2018 6.4
UserA 01/01/2018 05/01/2018 32 03/01/2018 6.4
UserA 01/01/2018 05/01/2018 32 04/01/2018 6.4
UserA 01/01/2018 05/01/2018 32 05/01/2018 6.4
If I add the UserB into the mix with the following records:
User Start End Hour
UserB 01/02/2018 02/02/2018 10
The table updates as follows:
User Start End Hour Dates CountPerDay
UserA 01/01/2018 05/01/2018 32 01/01/2018 6.4
UserA 01/01/2018 05/01/2018 32 02/01/2018 6.4
UserA 01/01/2018 05/01/2018 32 03/01/2018 6.4
UserA 01/01/2018 05/01/2018 32 04/01/2018 6.4
UserA 01/01/2018 05/01/2018 32 05/01/2018 6.4
UserB 01/02/2018 02/02/2018 10 01/02/2018 5
UserB 01/02/2018 02/02/2018 10 02/02/2018 5
I hope this helps
J

Related

Copy row of data from one pandas dataframe to another

A pandas newbie here. I imported an excel data into pandas, I want to copy subset of data of a specific row (placeholder) from one dataframe (Error_data1) to another dataframe (Error_data2) where the 'placeholder' exists.
Here is the first 4 rows of Error_data1 (it has 150 rows)
index student Error1 Error2 Error3 Error4 Error5
0 Henry 2.5647 -0.2145 1.3524 2.0124 6.2013
1 John -0.0124 1.0365 3.2145 4.0211 -5.0124
2 Terry 1.1120 2.2154 -6.2013 1.2032 2.3321
3 Gerald 9.2105 1.0212 3.2548 3.6478 4.1020
Here is the first 5 rows of Error_data2 (it has 358 rows)
index Day Time student Error1 Error2 Error3 Error4 Error5
0 Mon 01:00 Terry
1 Tue 05:15 John
2 Wed 05:25 john
3 Wed 12:15 Gerald
4 Thur 11:00 Henry
Here is the code i tried
for i in range(len(Error_data1)):
if Error_data1['Student'][i] == Error_data2['Student'][i]:
a = Error_data1.iloc[i,1:6]
Error_data2.iloc[i,4:9] = a
I expect Error_data2 to look like this:
index Day Time student Error1 Error2 Error3 Error4 Error5
0 Mon 01:00 Terry 1.1120 2.2154 -6.2013 1.2032 2.3321
1 Tue 05:15 John -0.0124 1.0365 3.2145 4.0211 -5.0124
2 Wed 05:25 john -0.0124 1.0365 3.2145 4.0211 -5.0124
3 Wed 12:15 Gerald 9.2105 1.0212 3.2548 3.6478 4.1020
4 Thur 11:00 Henry 2.5647 -0.2145 1.3524 2.0124 6.2013
You can try merging the two dataframes on student names.
combined = Error_data1.merge(Error_data2, on='student', how='left').fillna(0)

Get specific day of week of the upcoming Canada day on Linux

How to use cal command to add the calendar of next July to the end of the file, for example, myfile, and what day of the week the upcoming Canada Day fall on?
So far I just have this command:
cal July 2017 >> myfile
I feel like I am not doing it correct and I don't know which command to use, to find the day of the week for specific date.
Use this command:
cal 7 2017 >> file
The output is:
July 2017
Su Mo Tu We Th Fr Sa
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
You can find out day of week of a particular day with the GNU date command:
date -d"2017-07-01" # what day of week is Canada Day this year?
=> Sat Jul 1 00:00:00 UTC 2017
If you just want the week day, then:
date -d"2017-07-01" +%A
=> Saturday
You can check more about these commands with man cal or man date.
On a Mac, you could do this:
date -j -vJulm -v1d -v2017y +%A
See more on this post: date command on Mac OS

Count year occurred from datetime

I have data as below:
amos 50 10/16/2012 10:13
amri 50 1/9/2013 9:31
andi 10 3/11/2008 10:35
andik 10 12/6/2012 16:58
anggoro 50 9/13/2012 16:14
ari_prabowo 50 11/26/2012 10:30
astra_permana 10 8/2/2010 17:40
atang 10 1/16/2009 14:39
I want to counting how many times such like year of "2008", "2009", "2010", "2012", "2013" occurred.
Thanks for re-formatting your data.
I would add a helper column to calculate the year
=YEAR($A$1)
Then you can use a countif on that column
=COUNTIF($B$1:$B$10,2008)
Otherwise you could use a sumproduct (Excel 2003)
=SUMPRODUCT(($A$1:$A$10>=DATE(2008,1,1))*($A$1:$A$10<DATE(2009,1,1)))
Or countifs (excel 2007 +)
=COUNTIFS($A$1:$A$10,">=1/1/08",$A$1:$A$10,"<1/1/09")

Daily and Hourly Averages from (m/d/yyyy h:mm) timestamps in Excel

I have an Excel 2007 spreadsheet with date entries in this format m/d/yyyy h:mm (one cell). I would like find the hourly and daily average of all the columns of this spreadsheet and save each time aggregation to a new worksheet.
The data is recorded every ~10 minutes, but throughout the dates of data collection there was some time slips. Not every hour has the same number of rows. Also, the ending minute is either 0 or 6 depending on the time correction.
What would be a good way to approach this task within Excel 2007? It seems like this might be possible with a pivot table if I can create a formula that will select the correct range for the timestamps. Thanks.
For example, an date time entry in TIMESTAMP, 10/31/2012 0:06 which is in one cell.
TIMESTAMP Month Day Year Hour Min Rain_mm Rain_mm_2 AirTC AirTC_2 FuelM FuelM_2 VW ... there are ~16 variables (total) after the data time
10/31/2012 0:06 10 31 2012 0 06 0 0 26.11 26.08 2.545 6.4 0.049
10/31/2012 0:16 10 31 2012 0 16 0 0 25.98 25.97 2.624 6.6 0.049
10/31/2012 0:26 10 31 2012 0 26 0 0 24.32 23.33 2.543 6.5 0.048
10/31/2012 0:36 10 31 2012 0 36 0 0 24.32 23.33 2.543 6.5 0.048
10/31/2012 0:46 10 31 2012 0 46 0 0 24.32 23.33 2.543 6.5 0.048
10/31/2012 0:56 10 31 2012 0 56 0 0 25.87 25.87 2.753 7.3 0.049
10/31/2012 1:06 10 31 2012 0 06 0 0 25.74 25.74 2.879 8.1 0.051
## The above is just over one hour of collection on one day ##
...
## Different Day ### Notice Missing Time Stamp
11/30/2012 0:00 11 30 2012 0 06 0 0.1 26.12 26.18 2.535 6.4 0.049
11/30/2012 0:10 11 30 2012 0 16 0 0.1 25.90 25.77 2.424 6.6 0.049
11/30/2012 0:20 11 30 2012 0 26 0.1 0.2 24.12 24.43 2.542 6.4 0.046
11/30/2012 0:30 11 30 2012 0 36 0.1 0 24.22 22.32 2.543 6.5 0.048
11/30/2012 0:50 11 30 2012 0 56 0.1 0.2 26.77 25.87 2.743 6.3 0.049
11/30/2012 1:00 11 30 2012 0 06 0 0 24.34 24.77 2.459 5.1 0.050
## so forth on so on ##
After clarification of the requirement for daily averages edited to cover both daily and hourly averages:
Add a column (here B) for ‘H’ (ie hour) with =HOUR(A2) copied down.
(Note: Though formatted to show only m/d/y content of ColumnA is, in line with title, assumed to be all of mm/dd/yyyy hh:mm. Makes existing columns [with names jumbled] Month, Day, Year, Hour redundant).
Select data range.
Data, Subtotal, At each change in: TIMESTAMP, Use function: Average, Add subtotal to: check only columns G and to the right, OK.
Uncheck Replace current subtotals in Subtotal and apply At each change in: H, Use function: Average, and Add subtotal to: as before, OK.
Replace =SUBTOTAL(1, in Min column with =MIN( .
Delete ‘spare’ Grand Average row.
Reformat as required.
Hopefully this will be achieved and is what is required!:
Note midnight 'tonight' is counted as within first hour of tomorrow.
I had a similar need and worked it out this way:
Add a column for Date (assuming your dd/mm/yyyy hh:mm:ss data is in cell A2)
=DATE(YEAR(A2),MONTH(A2),DAY(A2))
Add a column for Year. If you have weeks from a single year, the year column can be neglected.
=YEAR(A2)
Add a column for Week Number
=WEEKNUM(A2)
Add 2 pivot tables, 1 for daily and 1 for weekly analysis.
Choose fields "Date" and the quantities you want. Put "Date" in the Rows section and sum/average of values in the Values section. You will get a date wise sum/average of the values you need.
In the weekly pivot table, do the same as above, just add "Year" and "Week no" in the Rows section instead of "Dates" as in above.
Hope this helps

MDB query for Time

I have table as
Id Name Date Time
1 S 1-Dec-2009 9:00
2 N 1-Dec-2009 10:00
1 S 1-Dec-2009 10:30
1 S 1-Dec-2009 11:00
2 N 1-Dec-2009 11:10
Need query to display as
Id Name Date Time
1 S 1-Dec-2009 9:00
1 S 1-Dec-2009 11:00
2 N 1-Dec-2009 10:00
2 N 1-Dec-2009 11:10
My backend database is MS Access and using VB6 for Max and Min time
I would make an additional two [int] columns, say hour and minute and then use an MS Access query to sort them. It would be MUCH easier to call that in VB. The query itself would be something like the following:
SELECT * FROM YOURTABLE ORDER BY id, hour, minute;

Resources