Forum Discussion
Fill a column based on a value in another column
- 8 years ago
Found a sloution to my problem which seems to work okay.
HSCIBStatus = VAR Most_Current_Year_Sem = CALCULATE(MAX(uNCPBIRepResultsAll[YearSem]), ALLEXCEPT(uNCPBIRepResultsAll,uNCPBIRepResultsAll[StudentID]))
RETURN CALCULATE(VALUES(uNCPBIRepResultsAll[StudentIBFlag]), FILTER(ALLEXCEPT(uNCPBIRepResultsAll, uNCPBIRepResultsAll[StudentID]), uNCPBIRepResultsAll[YearSem] = Most_Current_Year_Sem))
Thanks for all the help.
Basically, as a student moves through school, he may change IB status in Years 11 and 12. If I filter on the IB status = True then it removes all of his results from the junior years when he wasn't in IB. I want some way of saying, if he is now in the IB, set the IB to true for all the years he was at school so I can get all his results, even those in junior years.
Here is the link to my data. I want a formula that duplicates the last column - IB status
Thanks again for any help
Looking at your data its clear your trying apply an excel paradigm and actually build the tables with the data rather than let PowerBI filter data and build visuals. And your trying to take an output of an excel worksheet or pivot table that is derived and build a visual in powerBI. For example you have Last Semster and IB Status as value on each row. The last semsester can easily be calculated from the data) and is not needed and ideally you would have the course data in one table and the IB status in a lookup table with student, Year Semester, and IB Status. If your source data actually had this on every row for every class you woudl have to maintian it in multiple places.
I was able to get an IB status in a calculated column in your table but I also proposed an example of a DAX solution that does not require it.
I solved your IB Status column
- First I added a column for [Year SEM Number] this allows specifying a sort order for [Year Sem] and calculating the Last Year Semester using Last Year Semester = CALCULATE(MAX(Sheet1[Year Sem Number]),ALL(Sheet1)) this could have been skipped
- Created a few measures
Student Count = DISTINCTCOUNT(Sheet1[StudentID]) Students with IB in Last Semsester = CALCULATE([Student Count],Sheet1[StudentIBFlag],FILTER(Sheet1,Sheet1[Year Sem Number]=[Last Year Semester]))
- Built a dyamic table using DAX that lists the students and if they had IB Flag in the last semester or not
StudentIDs = VALUES(Sheet1[StudentID]) I then added a column with the measure Students with IB in last semester
I could have done this with a single DAX line StudentIDs = ADDCOLUMNS(VALUES(Sheet1[StudentID]),"IB In Last Semster",[Students with IB In Last Semster])
- Linked this StudentIDs table the Fact Table (Sheet1)
- Added calculated column IBStatus = RELATED(StudentIDs[IB in Last Semester])
I also included a demonstration where I built a different table with DAX to show the Students and the semesters that they had IB flag set to true. And then built a Matrix to diplay this table. Then a Matrix that shows the courses in each semester. I wrote a single DAX Measure to that calculates if any row in the table should be displayed or not.
Display Flag = CALCULATE(IF(HASONEVALUE('StudentIDs Semesters with IB'[StudentID]),1),ALL(Sheet1[ClassCode],Sheet1[Year Sem]))I then filtered the second Matix to only display where the Display flag is true. This has the effect that if you select any single student in the first matrix then the second matrix displays all the cources that student took regardless of IB Flag. If No Students or mor than one are selected the table is empty. Try clicking on the student in table 1 and they all will appear.
Both of these solutions will dynamically adjust to the data. I didn't elmimnat the IB Status flag in the base table to demonstrate using a lookup table of just the Student, Year Semester and IB Flag.
I know its alot but hopefully it gives you both a solution to satisfy your current approach but also gives you a perspective of how the paradigm of PowerBI is different and how much eaiser it is when you let PowerBI do its magic and how much easier it can be versus the way we had to do it in excel before Power Pivot or PowerBI.