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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
gelsonwirtijr
Frequent Visitor

contagem de anos consecutivos em DAX

Hello, everyone
I have the following table:

D2_DOCD2_CLIENTED2_CODF2_EMISSAOAnoEmissao

000015387000133SINAL SF208/02/20172017
000018277000133SINAL SF222/03/20182018
000027346000133SINAL SF210/11/20202020
000031179000133SINAL SF223/11/20212021
000031840000133SINAL SF328/02/20222022
000035941000133SINAL SF317/04/20232023


I would like to count the number of consecutive years starting from the current year (2023), for example, in the data set the DAX measure should return the value = 4, referring to the years 2023, 2022, 2021 and 2020.

gelsonwirtijr_0-1696249390353.png

 

2 REPLIES 2
Ahmedx
Super User
Super User

Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.

in DAX

https://1drv.ms/u/s!AiUZ0Ws7G26RiwCcmmoRwLm6ssTo?e=aS3xJl

lbendlin
Super User
Super User

Why should that be a DAX measure?  How can this be impacted by user actions?

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("dc89CsMwDAXgqxTPAes9ObUydgkUSpaMwfe/RqzY/RlqLXoIfQgdR5BamNVymDxDtYb9uT1et32lDy0KIwW+cbUydWbMQ0ZGUWfWmH0Ys6b7iEEiUBnlYrW9mQJ5GV7TztAYfpgl+cc8s/9GNsYvm5eEEUOOkpxpYxpKOQE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Doc = _t, Client = _t, Code = _t, Date = _t, Year = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Doc", Int64.Type}, {"Client", Int64.Type}, {"Code", type text}, {"Date", type text}, {"Year", Int64.Type}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "Consecutive", each List.Accumulate({0..[Index]},1,(state,current)=> if current = 0 then state else if #"Added Index"{current}[Year]=#"Added Index"{current-1}[Year]+1 then state+1 else 1))
in
    #"Added Custom"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done".

 

 

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

Find out what's new and trending in the Fabric community.