Finding chronological age on Filemaker to be used in equation - filemaker

I'm trying to calculate a chronological age based on a person's birth date (DOB) and the date of evaluation (DOE). I need the answer to be expressed as a numerical number that can be put in an equation. For example if a person's DOB is 12/26/07 and the DOE is 10/13/15 the chronological age will be 7.75, NOT 7;9 (7 years, 9 months). The decimal place needs to be a function out of 12, not simply representing the age in months. I have accurately calculated this but every time the chronological age is configured to be an even age (9 years 0 months) the calculation shows up as 90, I really need this "90" to be a "9.0" is there anything I can do to configure this into my calculation? The current calculation that is working for every other age is:
Year ( GetAsNumber ( DOE )) - Year ( DOB ) - If ( GetAsNumber ( DOE ) < Date ( Month ( DOB ) ; Day ( DOB ) ; Year ( GetAsNumber ( DOE ) ) ); 1 ; 0 ) & ( Mod ( Month ( GetAsNumber ( DOE ) ) - Month ( DOB ) + 12 - If ( Day ( GetAsNumber ( DOE ) ) < Day ( DOB ) ; 1 ; 0 ) ; 12 ) / 12)
Thank you!

Your Calculation is ok, but you are concatenating the 2 parts rather than adding them. That's why you get a "90" instead of a "9"
Also, if you want it to be "9.0", the calculation result must be numeric; and in layout mode set the format to decimal with a fixed number of decimals.
Round (
Year ( GetAsNumber ( DOE )) - Year ( DOB ) -
If (
GetAsNumber ( DOE ) < Date ( Month ( DOB ) ; Day ( DOB ) ; Year ( GetAsNumber ( DOE )));
1 ;
0)
+
( Mod (
Month ( GetAsNumber ( DOE ) ) - Month ( DOB ) + 12 -
If (
Day ( GetAsNumber ( DOE ) ) < Day ( DOB );
1;
0)
; 12 )
/ 12)
; 2 )

I would suggest a simpler approach:
Let (
elapsedMonths = 12 * ( Year ( DOE ) - Year ( DOB ) ) + Month ( DOE ) - Month ( DOB ) - ( Day ( DOE ) < Day ( DOB ) )
;
Div ( elapsedMonths ; 12 ) + Mod ( elapsedMonths ; 12 ) / 12
)
Note that this counts only fully elapsed months.

Related

Calculating Working Days The start date or end date in Calendar function can not be Blank value

I have a table in Power BI which shows Date In and Date Out of work completed, it's connected to a Calendar table with dates. I have 3 formula's to get actual working days after weekends and bank holidays etc. The trouble is however, it used to work and now yields the below error.
"The start date or end date in Calendar function can not be Blank
value."
Below are my formulas
Workdays (After Excluding Weekends Only) =
COUNTROWS (
FILTER (
ADDCOLUMNS (
CALENDAR ( 'Pro'[Date In], 'Pro'[Date Out] ),
"Day of Week", WEEKDAY ( [Date], 2 )
),
[Day of Week] <> 5
&& [Day of Week] <> 6
)
) - 1
Workdays (After excluding Holidays only) =
COUNTROWS (
FILTER (
ADDCOLUMNS(
CALENDAR ('Pro'[Date In], 'Pro'[Date Out]),
"Is Holiday", CONTAINS ('Holidays', 'Holidays'[Dates], [Date])
),
[Is Holiday] = FALSE()
)
)
Workdays (After excluding Weekends & Holidays) =
COUNTROWS (
FILTER (
ADDCOLUMNS(
CALENDAR ('Pro'[Date In], 'Pro'[Date Out]),
"Is Weekday", WEEKDAY ([Date], 2) < 6,
"Is Holiday", CONTAINS ('Holidays', 'Holidays'[Dates], [Date])
),
[Is Weekday] = TRUE() &&
[Is Holiday] = FALSE()
)
) - 1

Power BI Calculating Max Values filtered by DatesBetween

I am creating a comparison chart in Power BI for attendance (measured at 11:00) regarding Covid and I would like to get the maximum, minimum and average attendance for a previous lockdown, so a set period. (so the last 4 lines in the reproducible sample are not being used for any calculation). Could someone please help me?
In addition, I come from Python and use the print() and print(type()) command a lot for evaluation. Is there such a method in DAX, or how would you do this otherwise?
Minimal reproducible sample
Desired Result
Attempt:
/* START QUERY BUILDER */
DEFINE
VAR StartDate =
DATE ( 2020, 04, 12 )
VAR EndDate =
DATE ( 2020, 05, 30 )
EVALUATE
VAR MaxAttendance =
CALCULATE (
MAXX (
'DATA'[Employees present at 11:00],
FILTER (
'DATA'[Date],
DATESBETWEEN (
'DATA'[Date],
StartDate,
EndDate
)
)
)
)
VAR MinAttendance =
CALCULATE (
MINX (
'DATA'[Employees present at 11:00],
FILTER (
'DATA'[Date],
DATESBETWEEN (
'DATA'[Date],
StartDate,
EndDate
)
)
)
)
VARAverageAttendance =
CALCULATE (
AVERAGEX (
'DATA'[Employees present at 11:00],
FILTER (
'DATA'[Date],
DATESBETWEEN (
'DATA'[Date],
StartDate,
EndDate
)
)
)
)
RETURN
MaxAttendance
MinAttendance
AverageAttendance
/* END QUERY BUILDER */

Calculate Max and min dates

i'm trying to calculate the max ( and min )  of a date.
My situation is the following.
I've create a date table like this:
Date =
ADDCOLUMNS (
CALENDAR (DATE(2008;1;1); DATE(2020;12;31));
"DateAsInteger"; FORMAT ( [Date]; "YYYYMMDD" );
"Year"; YEAR ( [Date] );
"Monthnumber"; FORMAT ( [Date]; "MM" );"Daynumber"; FORMAT ( [Date]; "DD" );
"YearMonthnumber"; FORMAT ( [Date]; "YYYY/MM" );
"YearMonthShort"; FORMAT ( [Date]; "YYYY/mmm" );
"MonthNameShort"; FORMAT ( [Date]; "mmm" );
"MonthNameLong"; FORMAT ( [Date]; "mmmm" );
"DayOfWeekNumber"; WEEKDAY ( [Date] );
"DayOfWeek"; FORMAT ( [Date]; "dddd" );
"DayOfWeekShort"; FORMAT ( [Date]; "ddd" );
"Quarter"; "Q" & FORMAT ( [Date]; "Q" );
"YearQuarter"; FORMAT ( [Date]; "YYYY" ) & "/Q" & FORMAT ( [Date]; "Q" );
"WeekNum"; WEEKNUM ( [Date] )
)
I have a table  "Table 1" like  this :
ID ForeignKey Date
1 A 01/01/2005
2 A 05/04/2008
3 A 31/12/2019
4 B 15/3/2017
5 B 16/05/2018
6 B 15/04/2019
7 C 05/06/2006
8 C 04/12/2015
9 C 15/04/2019
 And another table "Table 2" like this
 
ID2 Price
A 100
B 500
C 650
The "Date" table  is related to "Table1" by date and Table 1 is related to  "Table 2" by Table1.ForeignKey = Table2.ID2.
In my report i have a date slicer which is set for example to filter dates between Jan 1 2008 and June 30 2018.
My goal is  to calculate the max and min date for each one of table 2 in the selected period like this: 
ID2 Price MinDate MaxDate
A 100 05/04/2008 05/04/2008
B 500 15/3/2017 16/05/2018
C 650 04/12/2015 04/12/2015
All i am able to achieve by doing somethig like this 
MaxDate= CALCULATE ( LASTDATE ( Table1[Date] ); FILTER (
ALLSELECTED('Date') ;
'Date'[Date] <= Max('Date'[Date])
))
is the max and min dates in  the whole calendar (01/01/2008 - 12/31/2020) , which is not what i'm trying to do .
How can I do this?
Thanks in advance.
With the relationships you describe, simply use measures:
MinDate = CALCULATE ( MIN ( 'Table 1'[Date] ) )
and
MaxDate = CALCULATE ( MAX ( 'Table 1'[Date] ) )
And set your slicer on 'Date'[Date]
See https://pwrbi.com/so_55373837/ for example PBIX file

How does this Time Difference Calculation work?

I wanted to display the difference in HH:MM:SS between two datetime fields in SQL Server 2014.
I found a solution in this Stack Overflow post. And it works perfectly. But I want to understand the "why" of how this arrives at the correct answer.
T-SQL:
SELECT y.CustomerID ,
y.createDate ,
y.HarvestDate ,
y.DateDif ,
DATEDIFF ( DAY, 0, y.DateDif ) AS [Days] ,
DATEPART ( HOUR, y.DateDif ) AS [Hours] ,
DATEPART ( MINUTE, y.DateDif ) AS [Minutes]
FROM (
SELECT x.createDate - x.HarvestDate AS [DateDif] ,
x.createDate ,
x.HarvestDate ,
x.CustomerID
FROM (
SELECT CustomerID ,
HarvestDate ,
createDate
FROM dbo.CustomerHarvestReports
WHERE HarvestDate >= DATEADD ( MONTH, -6, GETDATE ())
) AS [x]
) AS [y]
ORDER BY DATEDIFF ( DAY, 0, y.DateDif ) DESC;
Results:
1239090 2017-11-07 08:51:03.870 2017-10-14 11:39:49.540 1900-01-24 21:11:14.330 23 21 11
1239090 2017-11-07 08:51:04.823 2017-10-19 11:17:48.320 1900-01-19 21:33:16.503 18 21 33
1843212 2017-10-27 19:14:02.070 2017-10-21 10:49:57.733 1900-01-07 08:24:04.337 6 8 24
1843212 2017-10-27 19:14:03.057 2017-10-21 10:49:57.733 1900-01-07 08:24:05.323 6 8 24
The first column in Customer ID - the second and third columns are the columns I wanted to calculate the time difference between. The third column is the difference between the two columns - and one of the points in the code in which I do not understand.
If you subtract two datetime fields like this create date - harvestdate, why does it default to the year 1900?
And regarding DATEDIFF ( DAY, 0 , y.DateDiff) - what does the 0 mean? Does the 0 set the date as '01-01-1900'?
It works - for that I am grateful. I was hoping I could get an explanation as to why this behavior works?
I've added some comments that should explain it:
SELECT y.CustomerID ,
y.createDate ,
y.HarvestDate ,
y.DateDif ,
DATEDIFF ( DAY, 0, y.DateDif ) AS [Days] , -- calculates the number of whole days between 0 and the difference
DATEPART ( HOUR, y.DateDif ) AS [Hours] , -- the number of hours between the two dates has already been cleverly
-- calculated in [DateDif], therefore, all that is required is to extract
-- that figure using DATEPART
DATEPART ( MINUTE, y.DateDif ) AS [Minutes] -- same explanation as [Hours]
FROM (
SELECT x.createDate - x.HarvestDate AS [DateDif] , -- calculates the difference expressed as a datetime;
-- 0 is '1900-01-01 00:00:00.000' as a datetime, so the
-- resulting datetime will be that plus the difference
x.createDate ,
x.HarvestDate ,
x.CustomerID
FROM (
SELECT CustomerID ,
HarvestDate ,
createDate
FROM dbo.CustomerHarvestReports
WHERE HarvestDate >= DATEADD ( MONTH, -6, GETDATE ())
) AS [x]
) AS [y]
ORDER BY DATEDIFF ( DAY, 0, y.DateDif ) DESC;

Rolling sum per time interval per group

Table, data and task as follows.
See SQL-Fiddle-Link for demo-data and estimated results.
create table "data"
(
"item" int
, "timestamp" date
, "balance" float
, "rollingSum" float
)
insert into "data" ( "item", "timestamp", "balance", "rollingSum" ) values
( 1, '2014-02-10', -10, -10 )
, ( 1, '2014-02-15', 5, -5 )
, ( 1, '2014-02-20', 2, -3 )
, ( 1, '2014-02-25', 13, 10 )
, ( 2, '2014-02-13', 15, 15 )
, ( 2, '2014-02-16', 15, 30 )
, ( 2, '2014-03-01', 15, 45 )
I need to get all rows in an defined time interval. The above table doesn't hold a record per item for each possible date - only dates on which changes applied are recorded ( it is possible that there are n rows per timestamp per item )
If the given interval does not fit exactly on stored timestamps, the latest timestamp before startdate ( nearest smallest neighbour ) should be used as start-balance/rolling-sum.
estimated results ( time interval: startdate = '2014-02-13', enddate = '2014-02-20' )
"item", "timestamp" , "balance", "rollingSum"
1 , '2014-02-13' , -10 , -10
1 , '2014-02-15' , 5 , -5
1 , '2014-02-20' , 2 , -3
2 , '2014-02-13' , 15 , 15
2 , '2014-02-16' , 15 , 30
I checked questions like this and googled a lot, but didn't found a solution yet.
I don't think it's a good idea to extend "data" table with one row per missing date per item, thus the complete interval ( smallest date <-----> latest date per item may expand over several years ).
Thanks in advance!
select sum(balance)
from table
where timestamp >= (select max(timestamp) from table where timestamp <= 'startdate')
and timestamp <= 'enddate'
Don't know what you mean by rolling-sum.
here is an attempt. Seems it gives the right result, not so beautiful. Would have been easier in sqlserver 2012+:
declare #from date = '2014-02-13'
declare #to date = '2014-02-20'
;with x as
(
select
item, timestamp, balance, row_number() over (partition by item order by timestamp, balance) rn
from (select item, timestamp, balance from data
union all
select distinct item, #from, null from data) z
where timestamp <= #to
)
, y as
(
select item,
timestamp,
coalesce(balance, rollingsum) balance ,
a.rollingsum,
rn
from x d
cross apply
(select sum(balance) rollingsum from x where rn <= d.rn and d.item = item) a
where timestamp between '2014-02-13' and '2014-02-20'
)
select item, timestamp, balance, rollingsum from y
where rollingsum is not null
order by item, rn, timestamp
Result:
item timestamp balance rollingsum
1 2014-02-13 -10,00 -10,00
1 2014-02-15 5,00 -5,00
1 2014-02-20 2,00 -3,00
2 2014-02-13 15,00 15,00
2 2014-02-16 15,00 30,00