Join us for an expert-led overview of the tools and concepts you'll need to pass exam PL-300. The first session starts on June 11th. See you there!
Get registeredPower BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. 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.