Don't miss your chance to take the Fabric Data Engineer (DP-700) exam on us!
Learn moreNext up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now
Tengo una tabla llamada "Actividad" que me gustaría contar los ID de PRODUCT_CODE donde se selecciona el registro SEQN máximo para un identificador determinado.
Una muestra de los datos de la tabla Actividad.
Id | PRODUCT_CODE | SEQN |
1897 | AHA-3YR | 351319 |
1897 | AHA-3YR | 744415 |
1897 | AHA-3YR | 1132870 |
1914 | AHA-2YR | 266891 |
1914 | AHA-3YR | 488929 |
1914 | AHA-3YR | 878426 |
2038 | AHA-1YR | 190960 |
2038 | AHA-1YR | 638027 |
2038 | AHA-1YR | 857991 |
2038 | AHA-2YR | 259786 |
2038 | AHA-2YR | 475620 |
2038 | AHA-3YR | 990669 |
2557 | AHA-1YR | 190713 |
2557 | AHA-1YR | 255686 |
2557 | AHA-1YR | 333541 |
2557 | AHA-3YR | 989648 |
3555 | AHA-1YR | 279065 |
3555 | AHA-1YR | 415330 |
3555 | AHA-1YR | 537121 |
3555 | AHA-1YR | 656033 |
3555 | AHA-1YR | 1112955 |
3555 | AHA-2YR | 799658 |
El valor SEQN más reciente (MAX) determina el PRODUCT_CODE actual del ID. Encontré algunas buenas publicaciones del foro al devolver un valor Max basado en otra columna. Así escribió la siguiente instrucción DAX para crear una medida llamada LatestSEQN:
LatestSEQN =
VAR CurrentID = SELECTEDVALUE('Activity'[ID])
RETURN
MAXX(
FILTER(ALL('Activity'), Activity[ID] = CurrentID),
'Activity'[MaxSeq])
Esto me pasa a mí:
Id | PRODUCT_CODE | SEQN | LatestSEQN |
1897 | AHA-3YR | 351319 | 1132870 |
1897 | AHA-3YR | 744415 | 1132870 |
1897 | AHA-3YR | 1132870 | 1132870 |
1914 | AHA-2YR | 266891 | 878426 |
1914 | AHA-3YR | 488929 | 878426 |
1914 | AHA-3YR | 878426 | 878426 |
2038 | AHA-1YR | 190960 | 990669 |
2038 | AHA-1YR | 638027 | 990669 |
2038 | AHA-1YR | 857991 | 990669 |
2038 | AHA-2YR | 259786 | 990669 |
2038 | AHA-2YR | 475620 | 990669 |
2038 | AHA-3YR | 990669 | 990669 |
2557 | AHA-1YR | 190713 | 989648 |
2557 | AHA-1YR | 255686 | 989648 |
2557 | AHA-1YR | 333541 | 989648 |
2557 | AHA-3YR | 989648 | 989648 |
3555 | AHA-1YR | 279065 | 1112955 |
3555 | AHA-1YR | 415330 | 1112955 |
3555 | AHA-1YR | 537121 | 1112955 |
3555 | AHA-1YR | 656033 | 1112955 |
3555 | AHA-1YR | 1112955 | 1112955 |
3555 | AHA-2YR | 799658 | 1112955 |
Pero me di cuenta de que esto sólo crea un valor, y no hay ninguna relación entre LatestSEQN, PRODUCT_CODE, ID. ¿Te preguntas si me estoy acercando a este error? Mi salida deseada es:
Id | PRODUCT_CODE | LatestSEQN |
1897 | AHA-3YR | 1132870 |
1914 | AHA-3YR | 878426 |
2038 | AHA-3YR | 990669 |
2557 | AHA-3YR | 989648 |
3555 | AHA-1YR | 1112955 |
Así que puedo hacer un recuento de ID en PRODUCT_CODE para obtener:
PRODUCT_CODE | Contar |
AHA-1YR | 1 |
AHA-3YR | 4 |
gracias de antemano!
Hola @rlatham ,
Podrías intentarlo así:
LatestSEQN =
VAR CurrentID =
SELECTEDVALUE ( 'Activity'[ID] )
VAR MAX_SEQ =
MAXX (
FILTER ( ALL ( 'Activity' ), Activity[ID] = CurrentID ),
'Activity'[MaxSeq]
)
RETURN
IF ( MAX ( Activity[MaxSeq] ) = MAX_SEQ, MAX_SEQ, BLANK () )
Count 1 =
CALCULATE (
DISTINCTCOUNT ( Activity[ID] ),
FILTER (
ALLEXCEPT ( Activity, Activity[PRODUCT_CODE] ),
Activity[MaxSeq] = [LatestSEQN]
)
)
Count 2 = COUNTX(Activity,[LatestSEQN])
Muchas gracias Xue, lo clavaste!
Pregunta rápida si tienes tiempo. ¿Cuál es la diferencia en las funciones para el conde 1 y el recuento 2? ¿Usaría en diferentes condiciones? Cuando probé en el conjunto de datos más grande y el filtrado en otras condiciones obtengo diferentes recuentos. El número 2 parece ser el recuento preciso que necesito, aunque todavía validando esto.
Salud
Ryan
Hola @rlatham ,
Para "Cuenta 1", calculo en función del ID. Y para "Count 2", cuenta la medida "LatestSEQN". Escribo dos fórmulas porque no sé cómo les gustan tus datos reales. Si "Count 2" funciona para usted ahora, no es necesario agregar otras condiciones.
Eso es lo que pensé, pero quería asegurarme de que entendía tu lógica correctamente. Sí, quiero contar sólo "LatestSEQN". Gracias por aclarar.
If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.
A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.
Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.