Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join the FabCon + SQLCon recap series. Up next: Power BI, Real-Time Intelligence, IQ and AI, and Data Factory take center stage. All sessions are available on-demand after the live show. Register now

Reply
PleinCiel
New Member

[German] DATEDIFF rechnet immer ein Jahr zu viel

Hallo zusammen,

 

ich habe folgende Problematik.

 

Ich habe eine Tabelle in der zu jedem Mitarbeiter eine Spalte mit dem Hire Date und dem Exit Date (falls vorhanden - ansonsten leer) existiert.

 

Nun soll er mir in einer berechneten Spalte die Duration als ganze Zahl anzeigen. Hier ist aber das Problem das er immer ein Jahr zu viel nimmt. Sprich ein Mitarbeiter ist 103 Monate also ca. 8,6 Jahre da, dann soll er mir als Dauer 8 anzeigen, da er das neunte Jahr noch nicht vollendet hat.

 

Folgenden Code habe ich in den zwei Berechneten Spalten:

 

Last Year =
VAR __MaxYearHire = YEAR(MAXX(ALL('tbl_Employees'), 'tbl_Employees'[Hire Date]))
VAR __MaxYearExit = YEAR(MAXX(ALL('tbl_Employees'), 'tbl_Employees'[Exit Date]))
VAR __LastYear = MAX(__MaxYearHire, __MaxYearExit)
VAR __LastDate = DATE(__LastYear, 12, 31)
RETURN
__LastYear
 
und
 
Employment Duration =
VAR __MaxYearHire = YEAR(MAXX(ALL('tbl_Employees'), 'tbl_Employees'[Hire Date]))
VAR __MaxYearExit = YEAR(MAXX(ALL('tbl_Employees'), 'tbl_Employees'[Exit Date]))
VAR __LastYear = MAX(__MaxYearHire, __MaxYearExit)
VAR __LastDate = DATE(__LastYear, 12, 31)
RETURN
DATEDIFF(
    'tbl_Employees'[Hire Date],
    IF(ISBLANK('tbl_Employees'[Exit Date]), __LastDate, 'tbl_Employees'[Exit Date]),
    YEAR
)
 
Hat jemand eine Idee, wo der Fehler liegt.
1 ACCEPTED SOLUTION
Zanqueta
Super User
Super User

Hallo @PleinCiel,

Entschuldigen Sie bitte, aber ich musste die Übersetzung nach Deutschland schicken. Bitte ignorieren Sie Rechtschreibfehler.

Ich habe hier eine Situation mit den Daten simuliert und das Notwendige in eine berechnete Spalte eingegeben, um das Verständnis zu erleichtern. Sagen Sie mir bitte, ob Sie dies verwenden oder eine herkömmliche Methode.

zanqueta001_0-1762860665691.png

Employment Duration (Years) = 
VAR StartDate = 'tbl_Employees'[Hire Date]
VAR EndDate =
    IF(
        ISBLANK('tbl_Employees'[Exit Date]),
        DATE(YEAR(TODAY()), 12, 31),
        'tbl_Employees'[Exit Date]
    )
RETURN
INT(DATEDIFF(StartDate, EndDate, MONTH) / 12)



tbl_Employees:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("PY5BCsIwEEWvErKuZCZpbbuU7kRBdBm6CDSLQEgkrV7Io3gxZ6K4fMObx7dWomzkITlxdmULaSXCToFRGrAjAFSoCbSRc2OlptMxv19Z3EJ8uq8APdsDQXrEWD1DcA0bZX1aPFcpATtAgUBgQCFyVVe7pdPFLyWLKa9brdKGkas9vw4K6oa22jxrciU6ccr3X1vBnu3xv2H+AA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [EmployeelD = _t, Nome = _t, #"Hire Date" = _t, #"Exit Date" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Hire Date", type date}, {"Exit Date", type date}})
in
    #"Changed Type"

 

Wenn diese Antwort Ihr Problem gelöst hat, markieren Sie sie bitte als korrekt, um anderen Mitgliedern der Community zu helfen.

 

If this response was helpful in any way, I’d gladly accept a kudo.
Please mark it as the correct solution. It helps other community members find their way faster.
Connect with me on LinkedIn

View solution in original post

4 REPLIES 4
grazitti_sapna
Super User
Super User

Hi @PleinCiel,

 

I've used Google translate to answer your query as i don't know german, any spelling mistakes should be ignored,

 

Der Fehler bei der Jahresberechnung entsteht dadurch, dass die Differenz in Jahren und nicht in Tagen berechnet wird.

Siehe folgendes Beispiel:
DATEDIFF("2016-12-31", "2017-01-01", YEAR)
In diesem Beispiel gibt DAX trotz eines vergangenen Tages ein Jahr zurück, da sich das Jahr geändert hat. Versuchen Sie es mit folgendem DAX-Ausdruck:Employment Duration =
VAR HireDate = 'tbl_Employees'[Hire Date]
VAR ExitDate =
COALESCE('tbl_Employees'[Exit Date], TODAY())
VAR FullYears = DATEDIFF(HireDate, ExitDate, YEAR)
VAR AnniversaryThisYear =
DATE(YEAR(ExitDate), MONTH(HireDate), DAY(HireDate))
RETURN
IF(ExitDate < AnniversaryThisYear, FullYears - 1, FullYears)

 

🌟 I hope this solution helps you unlock your Power BI potential! If you found it helpful, click 'Mark as Solution' to guide others toward the answers they need.
💡 Love the effort? Drop the kudos! Your appreciation fuels community spirit and innovation.
🎖 As a proud SuperUser and Microsoft Partner, we’re here to empower your data journey and the Power BI Community at large.
🔗 Curious to explore more? [Discover here].
Let’s keep building smarter solutions together!

 

 

Zanqueta
Super User
Super User

Hallo @PleinCiel,

Entschuldigen Sie bitte, aber ich musste die Übersetzung nach Deutschland schicken. Bitte ignorieren Sie Rechtschreibfehler.

Ich habe hier eine Situation mit den Daten simuliert und das Notwendige in eine berechnete Spalte eingegeben, um das Verständnis zu erleichtern. Sagen Sie mir bitte, ob Sie dies verwenden oder eine herkömmliche Methode.

zanqueta001_0-1762860665691.png

Employment Duration (Years) = 
VAR StartDate = 'tbl_Employees'[Hire Date]
VAR EndDate =
    IF(
        ISBLANK('tbl_Employees'[Exit Date]),
        DATE(YEAR(TODAY()), 12, 31),
        'tbl_Employees'[Exit Date]
    )
RETURN
INT(DATEDIFF(StartDate, EndDate, MONTH) / 12)



tbl_Employees:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("PY5BCsIwEEWvErKuZCZpbbuU7kRBdBm6CDSLQEgkrV7Io3gxZ6K4fMObx7dWomzkITlxdmULaSXCToFRGrAjAFSoCbSRc2OlptMxv19Z3EJ8uq8APdsDQXrEWD1DcA0bZX1aPFcpATtAgUBgQCFyVVe7pdPFLyWLKa9brdKGkas9vw4K6oa22jxrciU6ccr3X1vBnu3xv2H+AA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [EmployeelD = _t, Nome = _t, #"Hire Date" = _t, #"Exit Date" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Hire Date", type date}, {"Exit Date", type date}})
in
    #"Changed Type"

 

Wenn diese Antwort Ihr Problem gelöst hat, markieren Sie sie bitte als korrekt, um anderen Mitgliedern der Community zu helfen.

 

If this response was helpful in any way, I’d gladly accept a kudo.
Please mark it as the correct solution. It helps other community members find their way faster.
Connect with me on LinkedIn

Vielen Dank für die Hilfe.

Hans-Georg_Puls
Super User
Super User

Hallo @PleinCiel ,

um das genau zu verstehen wäre es spannend zu wissen, welcher Datentyp den Spalten zugeordnet ist. DATEDIFF liefert eine Dezimalzahl. An irgendeiner Stelle erfolgt dann offenbar eine Rundung. Um sicher zu gehen, dass das gewünschte passiert, wäre es hier sinnvoll direkt in der Formel eine entsprechende Anpassung vorzunehmen. Das wäre dann vermutlich in INT() um den DATEDIFF Ausdruck. Damit ist klar, dass immer abgerundet werden soll auf die nächste ganze Zahl.

 

Hoffe ich habe das Problem richtig verstanden und konnte helfen...

Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

Check out the April 2026 Power BI update to learn about new features.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

FabCon and SQLCon Highlights Carousel

FabCon &SQLCon Highlights

Experience the highlights from FabCon & SQLCon, available live and on-demand starting April 14th.